How to use the where method of ActiveRecord :: QueryMethods to restrict the search?

I am using Ruby on Rails 3, and I would like to limit the search to the method wherefrom 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 request only 10 records. How can i do this?


The RoR source code has:

def where(opts, *rest)
  relation = clone
  relation.where_values += build_where(opts, rest) unless opts.blank?
  relation
end
+3
source share
2 answers

Your second example works:

Users.where(:name => "Test_name").limit(10)

This Rails manual has a good list and explanation of all query methods: http://guides.rubyonrails.org/active_record_querying.html

+7
source

I do not think you can do it where. You can only do

Users.where(:name => "Test_name").limit(10)

: limit :

Users.find(:all, :conditions => ["name = 'Test_name'"], :limit => 10)
+1

All Articles