Ruby on Rails
Deprecated Patterns
The Rails framework is changing at a breathtaking pace, due in large part to its rapidly rising popularity. This can make life difficult for authors of books on Rails, as well as those who try to learn from these books as some of the code examples do not age well. With that in mind, this entry serves as a reference to practices and patterns that may have appeared in print or in core code, but are now considered “deprecated.” As Ferris Bueller said, “Life moves pretty fast; if you don’t stop and look around once in awhile, you might miss it.”
- With Rails 1.0 and up, transactional fixtures are recommended. When using this style of fixtures the data is no longer loaded into an instance variable for you (this is a configurable option). For a fixture :products, instead of @products[“wheatabix”] use products(:wheatabix).
- Accesses on the instance variables @session, @params, @flash,@request, and @response should be replaced by calls to session, params, etc. Reading the instance variable directly is bad OOP practice, whereas accessing them via the associated method is more OOPy and flexible.
- HABTM relationships with attributes that used push_with_attributes should be promoted to full models by using has_many :through. The old way was more of a hack than a feature; has_many :through is semantically cleaner and has more features.
- The find_all() and find_first() finders have been replaced by find(:all) and find(:first). This DRYs up the Rails code and makes the find() method a little more flexible for you.
- In the same vein, render_partial has been supplanted by render :partial. The same reasoning given for find_all/first() applies here.
- Components in general have been de-emphasized since the rise of plugins and engines. While they do have their uses, the speed penalty they incur can be significant. Consider whether or not a simple render :partial will do.
- Symbols are now preferred over Strings for specifying actions: Instead of link_to :action => ‘index’ use link_to :action => :index. Using Strings incurs a slight performance hit as Ruby must translate the string back into a Symbol before using it. This preference extends to most places where strings were formerly used to refer to things in Rails, for example in render calls.
- model :user has been replaced by require_dependency ‘user’ according to http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance (I think).
External Resources
- Things You Shouldn’t be Doing in Rails and a followup
- RailsCheck – Q/A gem for Rails projects that – among other things – detects deprecated code and warns you about it. Lists all the warnings from deprecated plugin below and many more. Also reports on a number of typical bugs, potential problems and inconsistencies in Rails projects.
- Deprecated Plugin – nice little tool that searches for deprecated code and calls it out for you. Note that start_form_tag can be converted to form_tag do (not form_for, as they say), and that a nice paginator exists in Bruce Williams’ paginator.