Rails 3 : Anticipating migration for 2.3 beginners
coding-style, ruby-on-rails, upgrade
Solution
Looking at my personal coding habits (I have been using Rails since 1.2.x), here's a list of API changes you can anticipate according to Rails 3 release notes.
find(:all)
Avoid the usage of:
Model.find(:all)
Model.find(:first)
Model.find(:last)
in favour of:
Model.all
Model.first
Model.last
Complex queries
Avoid the composition of complex queries in favor of named scopes.
Anticipate Arel
Rails 3 offers a much cleaner approach for dealing with ActiveRecord conditions and options. You can anticipate it creating custom named scopes.
class Model
named_scope :limit, lambda { |value| { :limit => value }}
end
# old way
records = Model.all(:limit => 3)
# new way
records = Model.limit(3).all
# you can also take advantage of lazy evaluation
records = Model.limit(3)
# then in your view
records.each { ... }
When upgrading to Rails 3, simply drop the named scope definition.
Constants
Avoid the usage of the following constants in favour of the corresponding `Rails.x` methods, already available in Rails 2.x.
- `RAILS_ROOT` in favour of Rails.root,
- `RAILS_ENV` in favour of Rails.env, and
- `RAILS_DEFAULT_LOGGER` in favour of Rails.logger.
Unobtrusive Javascript
Avoid heavy JavaScript helpers in favour of unobtrusive JavaScript.
Gem dependencies
Keep your `environment.rb` as clean as possible in order to make easier the migration to Bundler. You can also anticipate the migration using Bundler today without Rails 3.
Problem
I am a beginner in Rails. I use 2.3.X. I just saw Rails 3 is pre-released [edit: now in release candidate!]. I will most probably eventually switch to it. What are the common coding habits in 2.3 I should not take, so that the switch is as smooth as possible ? Edit: I've done my homework and read the Release notes. But they are far from clear for the most crucial points, for example : 1.5 New APIs Both the router and query interface have seen significant, breaking changes. There is a backwards compatibility layer that is in place and will be supported until the 3.1 release. This is not comprehensive enough for a beginner like me. What will break ? What could I do already in 2.3.X to avoid having troubles later ?