Welcome to End Point’s blog

Ongoing observations by End Point people

Rails Tips & Tricks at RailsConf 2014

Rails: They start so young!

One of the talks I attended on Day two of RailsConf 2014 was Tricks that Rails didn't tell you about by Carlos Antonio. As the title suggests, Carlos covered a number of Rails items that are either not widely used or not well documented. I've taken the lazy approach and listed out all the topics he covered, but provided documentation or relevent links to all of the tricks in case you'd like to learn more.



  • Relation.merge, e.g. Product.joins(:reviews).merge(Review.approved), allows you to utilize scope from another model rather than passing in exact SQL conditional, i.e. limiting knowledge of Review to Product.
  • Utilize group counting to group results by a column and get the count. This also accepts multiple fields to group.
  • relation.first! and relation.last! are similar to first & last but raise exception RecordNotFound if there are no records.
  • where.not is a cool finder trick, e.g. scope :some_scope, -> { where.not status: 'draft' }
  • You can control eager or lazy loading in Rails via eager_load and preload. These are a little tricky to sum up in a small example, so hopefully those links provide more.
  • Instead of passing in SQL in finder methods or scopes, you can call with :desc, e.g. scope :some_scope, -> { order created_at: :desc }
  • pluck is a wonderful tool. e.g. Product.pluck(:name), but also Product.pluck(:name, :price) will retrieve product name and pricing information.
  • to_param allows you to override the default of using an object's id to a custom finder key.
  • The difference between exists?/any? and present? is that the former two will first look at the database and then loop through, while the latter will look at the database once and does not require a second look.
  • ActiveRecord's "baked-in" benchmark allows you to benchmark blocks for runtime analysis

Active Model

  • include ActiveModel::Model in a class to allow Rails form helpers to be used on instances of that class, as well as leverage ActiveModel validation methods.

Action Mailer

  • Rails mailers come with some built in mapping between i18n defaults and the mailer language, class, method, value. This is a really hard thing to Google for, so I suggest to read all the things.

Action View

  • content_tag_for builds HTML for a loop of objects.
  • render(collection) returns false if a collection is empty, so rather than clouding your view with an if / else block, you can do something like: render(collection) || content_tag(:p, 'No Article Found')
  • Learn more about local_assigns. I didn't quite understand this tip in the talk but I found a Stack Overflow qna on the subject.
  • truncate does what you'd expect, truncate text after a number of characters.
  • Rails locales have a *_html method automatically which allows you to pass HTML and does not require you specify raw(that content) when you use the content. See above about reading all the things in i18n.
  • benchmark in AV allows you to benchmark view rendering

Action Controller

  • You can redirect with params, e.g. redirect('/articles/%{id}').
  • config.exceptions_app allows you to define your own custom routes for application exceptions.


  • In the Rails console, app is an available variable which can make requests.
  • In the Rails console, the helper variable can be used to access Rails helpers available in your views.
  • Running rails console --sandbox will rollback data changes after the console is exited


  • rake notes will list comments marked TODO, FIXME, OPTIMIZE, and has the ability to generate custom annotations

Updating Rails

  • When upgrading Rails, rake rails:update reports on diffs in all configuration files and helps you work through conflicts

No comments: