CSC/ECE 517 Fall 2011/ch2 2f jm

From Expertiza_Wiki
Jump to navigation Jump to search

Rails 2 vs. Rails 3

Introduction

This is an introduction to the subject.

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 of these statements:

# 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, the deprecated statements are

# 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”)

The correct statements should be

# good 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 go here.


Note

2f. Rails 2 vs. Rails 3. Describe the differences between Rails 2 and Rails 3, but pitch your coverage at a student in this class. Describe the differences in the order that we cover them in class, and use the code covered in class to explain the differences.