I have a search form that provides a param to a find condition. I have a projects model with multiple joins to other models: has_many :accomplishments has_many :cooperatorships has_many :sponsorships has_many :cooperators, :through => :cooperatorships has_many :sponsors, :through => :sponsorships My search when setup like this works: def self.search(search) if search find(:all, :conditions => [''projects.name LIKE ? OR projects.description LIKE ? OR projects.leadName LIKE ? OR sponsors.name LIKE ?'', "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%"], :include => :sponsors ) else scoped end end If I attempt to add another include like: :include => {:sponsors, :cooperators} ActiveRecord reports an error that it can''t find the relation and perhaps I''ve spelled it incorrectly. I can switch out :sponsors for :cooperators and change the query and it finds the relation just fine. Is it possible to do multiple joins or "includes" in this way? Thanks, -Jim -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 2, 2011, at 10:10 AM, Jim Wharton wrote:> I have a search form that provides a param to a find condition. I have a > projects model with multiple joins to other models: > > has_many :accomplishments > has_many :cooperatorships > has_many :sponsorships > has_many :cooperators, :through => :cooperatorships > has_many :sponsors, :through => :sponsorships > > > My search when setup like this works: > > def self.search(search) > if search > find(:all, :conditions => [''projects.name LIKE ? > OR projects.description LIKE ? > OR projects.leadName LIKE ? > OR sponsors.name LIKE ?'', > "%#{search}%", > "%#{search}%", > "%#{search}%", > "%#{search}%"], > :include => :sponsors > ) > else > scoped > end > end > > If I attempt to add another include like: > > :include => {:sponsors, :cooperators}:include => [:sponsors, :cooperators] You want an array, not a hash.> ActiveRecord reports an error that it can''t find the relation and > perhaps I''ve spelled it incorrectly. > > I can switch out :sponsors for :cooperators and change the query and it > finds the relation just fine. > > Is it possible to do multiple joins or "includes" in this way? > > Thanks, > -Jim > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom wrote in post #996237:> On May 2, 2011, at 10:10 AM, Jim Wharton wrote: > >> My search when setup like this works: >> "%#{search}%"], >> :include => :sponsors >> ) >> else >> scoped >> end >> end >> >> If I attempt to add another include like: >> >> :include => {:sponsors, :cooperators} > > :include => [:sponsors, :cooperators] > > You want an array, not a hash.Awesome, thank you. I''ve gone with a "where" now as I''ve read that the "find" syntax is being deprecated. Thanks again! -Jim -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.