I have a drop down list that has a value ''any''. And I have this code in controller... @results = Post.find(:all, :conditions => ''status = ?'', @status) If any is selected, it means search for all records. What do I need to put in variable @status to make so that it would search for all. I can have multiple drop down lists in future with many combinations so I cannot if statement here. Thanks -- 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 22 Oct 2007, at 11:09, Vapor .. wrote:> > I have a drop down list that has a value ''any''. > And I have this code in controller... > > @results = Post.find(:all, :conditions => ''status = ?'', @status) >There is no such value.> If any is selected, it means search for all records. What do I need to > put in variable @status to make so that it would search for all. > > I can have multiple drop down lists in future with many > combinations so > I cannot if statement here. >Oh yes you can :-) You just need to build up your conditions parameter bit by bit, eg conditions = {} if @status != ''any'' conditions[:status] = @status end if @foo != ''any'' conditions[:foo] = @foo end @results = Post.find :all, :conditions => conditions You probably want to put most of this in the model. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> conditions = {} > if @status != ''any'' > conditions[:status] = @status > end > if @foo != ''any'' > conditions[:foo] = @foo > end > @results = Post.find :all, :conditions => conditionsThanks Fred :) -- 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 -~----------~----~----~----~------~----~------~--~---