Hello: I''m pretty new to mysql and need to build a search function that searches fields in a ''case'' object as well as fields in ''person'' objects that reference each ''case'' object. The function must return a single array of ''case'' objects. Searching for fields in the case object seems pretty straightforward: def search @cases = Case.find(:all, :conditions => ["lower(field_1) like ? OR lower(field_2) like ?", "%" + params[:search].downcase + "%", "%" + params[:search].downcase + "%"]) render :partial => ''cases'', :collection => @cases end But I can''t figure out how to search objects that ''belong_to'' the case object. -- 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 -~----------~----~----~----~------~----~------~--~---
> But I can''t figure out how to search objects that ''belong_to'' the case > object. > -- > Posted viahttp://www.ruby-forum.com/.It seems like what you are trying to do is search the person fields for a string. That is going to return Person records. You then want an array of the cases associated with the persons that were returned from the search string. Is that correct? Greg DeVore --~--~---------~--~----~------------~-------~--~----~ 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 Dec 2007, at 00:41, Peter Marks wrote:> > Hello: > > I''m pretty new to mysql and need to build a search function that > searches fields in a ''case'' object as well as fields in ''person'' > objects > that reference each ''case'' object. The function must return a single > array of ''case'' objects. > > Searching for fields in the case object seems pretty straightforward: > > def search > @cases = Case.find(:all, :conditions => ["lower(field_1) like ? OR > lower(field_2) like ?", "%" + params[:search].downcase + "%", "%" + > params[:search].downcase + "%"]) > render :partial => ''cases'', :collection => @cases > end > > But I can''t figure out how to search objects that ''belong_to'' the case > object.You can do Case.find :all, :include => :person, :conditions => [''cases.foo = ? or people.bar = ?'', ...] In rails 2 you can use :joins instead of :include if you''re not interested in actually eager loading the :person associations. And you''ve always been able to Case.find :all, :joins => "some fancy bit of sql to join the people table in the desired way", :conditions => [''cases.foo = ? or people.bar = ?'', ...] (you almost always want to specify a :select option if you do tha) 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> You can do Case.find :all, :include => :person, :conditions => > [''cases.foo = ? or people.bar = ?'', ...] > In rails 2 you can use :joins instead of :include if you''re not > interested in actually eager loading the :person associations. > And you''ve always been able to Case.find :all, :joins => "some fancy > bit of sql to join the people table in the desired way", :conditions > => [''cases.foo = ? or people.bar = ?'', ...] > (you almost always want to specify a :select option if you do tha) > > FredThis is exactly what I was looking for. God I love rails. Thanks a lot guys. Peter -- 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 -~----------~----~----~----~------~----~------~--~---