CSC/ECE 517 Fall 2011/ch2 2f jm
Rails 2 vs. Rails 3
Introduction
There are many changes involved when you migrate your Rails application from Rails 2 to Rails 3. If your application is a based on Rails 2, there are important changes you'll need to make to run your application in Rails 3.<ref>Carletti, Simone. "The Road to Rails 3: make your Rails 2.3 project more Rails 3 oriented" http://www.simonecarletti.com/blog/2010/07/the-way-to-rails-3/</ref> There are additional changes planned for Rails 3.1 and Rails 3.2 that you should consider now if you're migrating from Rails 2 to Rails 3.<ref>Naik, Pratik. "Active Record Query Interface 3.0" http://m.onkey.org/active-record-query-interface</ref> A variety of these changes, including changes covered in class, are covered in the sections below.
Differences between Rails 2 and Rails 3
Example Code differences between Rails 2 and Rails 3 (from class)
Deprecated Methods in Active Record Query Interface for Rails 3
At this point you may have discovered some of the great functionalities that come with Rails 3. When you start to switch from Rails 2 to Rails 3, you may start to see warning messages or even errors in some of your existing methods. That is because in Rails 3, all of the internals, especially the ActiveRecord interface, received a massive clean out, which makes the Rails modularity framework much cleaner and more extensible for developers to use. As this result, some of the methods in Rails 2 are now deprecated or even fully removed, which causes warning messages or error when compiling the code. Below will show a list of these deprecated methods.
find([:all | :first | :last]) Method
The find(*args) method in the Active Record interface allows user to retrieve objects from the database table. User can retrieve the first or the last object in the table, or an object by its id, or all the objects at once. These methods followed the syntax of #find(:all), #find(:first) or #find(:last)in Rails 2. However in Rails 3, they are now deprecated and have been converted to #all, #first, and #last alternatives. See the following example for comparison:
# the deprecated version @categories = Category.find(:all) @first_category = Category.find(:first) @last_category = Category.find(:last) # the good version @categories = Category.all @first_category = Category.first @last_category = Category.last
find(*args, options) Method
In Rails 3.1, ActiveRecord methods that have Options argument are also deprecated and will be removed in Rails 3.2. These passing options hash includes :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock. So what is the replacement for the find(*args, options)method? The answer is inside the new Rails 3 API. The new Rails 3 API has the following finder methods to replace with the find(*args, options)method.
- where (:conditions)
- having (:conditions)
- select
- group
- order
- limit
- offset
- joins
- includes (:include)
- lock
- readonly
- from
For examples, in Rails 2 you do
# deprecated statements @desert_categories = Category.find(:all, :conditions => {:name => "Desert"}) @two_categories = Category.find(:all, :limit => 2) @ordered_categories = Category.find(:all, :order => “created_at desc”)
But in Rails 3, you should now do
# correct version @desert_categories = Category.where(:name=>"Desert") @two_categories = Category.limit(2) @ordered_categories = Category.order("created_at desc")
Furthermore, all of the above methods are defined on the Relation object, which gains the ability of chainability. In other word, all the methods can be chained together. This is a very powerful feature because it allows you to define multiple conditions in a find statement using just one line of code, which makes developers’ life so much easier. For example, if you want to return the 10 newest Desert recipes in descendent order based on the creation date, you can do
@recipes = Recipe.where(:category_name => “Desert”). order("created_at desc").limit(10)
named_scope Method
The named_scoped method is deprecated and needed to be renamed to scope in Rails 3. For instance, a definition like :
class Category name_scope :desert, :conditions => {:name => “Desert”} name_scope :fruit, :condition => {:name => “Fruit”} end
Will become :
class Category scope :desert, :conditions => {:name => “Desert”} scope :fruit, :condition => {:name => “Fruit”} end
However, since using options hash is deprecated in Rails 3.1, therefore the new finder methods should be used in this case:
class Category scope :desert, where {:name => “Desert”} scope :fruit, where {:name => “Fruit”} end
At the end when you call these named scopes, you can simply do
desert = Category.desert fruit = Category.fruit
References
<references/>