10 Things You Didn’t Know Rails Could do
May 15, 2012
James Edward Gray 2 gave a presentation about hidden skills of Rails. Here are the ones I found most interesting:
- Run from a single file
- Remind you of things via “rake notes”
- Sandbox the console via “rails console –sandbox” (reverts all changes on exit)
- Run helper methods in the console via “helper.number_to_currency(100)”
- Use non-webrick servers in development via “rails s thin” (this is shorter than “bundle exec thin start”!)
- Show you the DB’s migration status via “rake db:migrate:status” to see which migrations have run.
- Pluck fields from the DB: “User.pluck(:email)” instead of “User.select(:email).map(&:email)”
- Count DB records in groups via “Event.group(:trigger).count” => { “edit” => 3, “view” => 10 }
- Override associations via “belongs_to :owner; def owner=(new_owner); self.previous_owner = owner; super; end”
- Use limitless strings in PostgreSQL (via Josh Susser)
- Use a different database for each user.
- Merge nested hashes via Hash#deep_merge
- Remove specific keys from a hash via “params.except(:controller, :action)”
Link: 10 Things You Didn’t Know Rails Could do // Speaker Deck via speakerdeck.com