Ruby on Rails Monday, May 2, 2011

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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment