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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 19 Feb 2008, at 21:37, Jonathan Rochkind wrote:> > 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> > > 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? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Feb 20, 5:51 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 19 Feb 2008, at 21:37, Jonathan Rochkind wrote: > > > > > 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 > > > > > 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? > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@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. On Feb 20, 12:02 pm, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> wrote:> 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 > > On Feb 20, 5:51 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 19 Feb 2008, at 21:37, Jonathan Rochkind wrote: > > > > 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 > > > > 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? > > > -- > > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks! I had missed that somehow, it is clear now. Certainly a very useful interface to ''find'', I agree. AndyV wrote:> @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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---