How to use the 'where' method from 'ActiveRecord::QueryMethods' in order to limit searching?
I am using Ruby on开发者_Python百科 Rails 3 and I would like to limit a search in a where
method from ActiveRecord::QueryMethods
using something as the following
Users.where(:name => "Test_name", :limit => 10)
Users.where(:name => "Test_name").limit(10)
That is, I would like to query only 10 records. How I can do that?
In the RoR source code there is:
def where(opts, *rest)
relation = clone
relation.where_values += build_where(opts, rest) unless opts.blank?
relation
end
Your second example works:
Users.where(:name => "Test_name").limit(10)
There is a nice list and explanation of all the query methods in this Rails guide: http://guides.rubyonrails.org/active_record_querying.html
I don't think you can do that in where. You can only do
Users.where(:name => "Test_name").limit(10)
or if you insist to use :limit in other selector method:
Users.find(:all, :conditions => ["name = 'Test_name'"], :limit => 10)
精彩评论