Rails 3 Active Record usage in pills
In Rails 3.1 passing options hash containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock to any of the ActiveRecord provided class methods will be deprecated, and completly removed on Rails 3.2.
The following shows a few example of the deprecated usages:
User.find(:all, :limit => 1)
User.find(:all)
User.find(:first)
User.first(:conditions => {:name => 'lifo'})
User.all(:joins => :items)
Many other useful things will be deprecated:
named_scope :red, :conditions => { :colour => 'red' }
named_scope :red, lambda {|colour| {:conditions => { :colour => colour }} }
with_scope(:find => {:conditions => {:name => 'lifo'}) { ... }
with_exclusive_scope(:find => {:limit =>1}) { ... }
default_scope :order => "id DESC"
red_items = Item.scoped_by_colour('red')
red_old_items = Item.scoped_by_colour_and_age('red', 2)
ActiveRecord in Rails 3 will have the following new finder methods:
where (:conditions)
select
group
order
limit
joins
includes (:include)
lock
readonly
from
Some examples of the new usage:
lifo = User.where(:name => 'lifo')
new_users = User.order('users.id DESC').limit(20).includes(:items)
cars = Car.where(:colour => 'black')
rich_ppls_cars = cars.order('cars.price DESC').limit(10)
Lazy Loading
As it might be clear from the examples above, relations are loaded lazily – i.e you call an enumerable method on them. This is very similar to how associations and named_scopes already work.
cars = Car.where(:colour => 'black') # No Query
cars.each {|c| puts c.name } # Fires "select * from cars where ..."
This is very useful along side fragment caching. So in your controller action, you could just do :
def index
@recent_items = Item.limit(10).order('created_at DESC')
end
And in your view :
<% cache('recent_items') do %>
<% @recent_items.each do |item| %>
...
<% end %>
<% end %>
In the above example, @recent_items are loaded on @recent_items.each call from the view. As the controller doesn’t actually fire any query, fragment caching becomes more effective without requiring any special work arounds.
New scopes
named_scope have now been renamed to just scope.
Using the method named_scope is deprecated in Rails 3.0. But the only change you’ll need to make is to remove the “named_” part. Supplying finder options hash will be deprecated in Rails 3.1.
So a definition like :
class Item
named_scope :red, :conditions => { :colour => 'red' }
named_scope :since, lambda {|time| {:conditions => ["created_at > ?", time] }}
end
Now becomes :
class Item
scope :red, :conditions => { :colour => 'red' }
scope :since, lambda {|time| {:conditions => ["created_at > ?", time] }}
end
However, as using options hash is going to be deprecated in 3.1, you should write it using the new finder methods :
class Item
scope :red, where(:colour => 'red')
scope :since, lambda {|time| where("created_at > ?", time) }
end
Comments
-
I guess this will be a bitter pill to swallow for many Rails developers who got used to the old find :first and find :all syntax.

