ActiveRecord.find with hash?

The Agile Development book suggests one can pass a hash to the find class method on an ActiveRecord entity:

hash = { :firstname => "john", :lastname => "smith" } Customer.find(:all, hash)

However, this doesn't seem to actually work.

Is there any way tod o this? I have a hash with keys that symbols matching valid attributes of the entity, and values that are the values I want to match. Surely there is some ActiveRecord method to find objects based on this, no?

The Agile Development book suggests one can pass a hash to the find class method on an ActiveRecord entity:

hash = { :firstname => "john", :lastname => "smith" } Customer.find(:all, hash)

However, this doesn't seem to actually work.

Customer.find :all, :conditions => hash

I'd go even one deeper: Customer.find(:all, hash)

I've been using code like this more often in RESTful controllers. By accumulating the find options for the #index action I can use it to serve several purposes. The most obvious example would be for filter +paging:

requested_page = params[:page] || 0 find_options = {:order=>:name, :offset=>requested_page * page_size, :limit=>page_size} find_options[:conditions] = ['name LIKE ?', "#{params[:name]}"] unless params[:name].blank? Customer.find(:all, find_options)

The important thing to be aware of is that the hash to be passed to ARec#find comes *after* a required first parameter (symbols :first or :all, an id, an array of ids).

HTH, AndyV

@Jonathan -- sorry, I didn't read yours closely enough. The thing that you're missing is that the parameters for the sql query are added to the hash keyed by :conditions. Hopefully the rest of my response clears that up.

Thanks! I had missed that somehow, it is clear now. Certainly a very useful interface to 'find', I agree.

AndyV wrote: