Find across multiple models

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

On May 2, 2011, at 10:10 AM, Jim W. 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.

Philip H. wrote in post #996237:

On May 2, 2011, at 10:10 AM, Jim W. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs