[Rails] Query on many to many relation
Van Aken Martin (GFDI)
Martin.VanAken at gfdi.be
Tue Jan 25 09:01:27 GMT 2005
Thanks already, Victor.
I see that I must be a little more precise about my problem.
The condition of being an employee of the given company is only one
among a series of other.
>From your proposal, I could without problem fetch all employees from the
given company, then apply my other criteria.
But doing so would be very static (I construct my multi-criteria query
dynamically), and would require to load from the DB a lot of records I
don't need.
For example, one of my (possible) criteria is being part of a team, and
my code is :
if(team_key != '')
list << team_key
query << " AND " unless query == ""
query << "team_id = ?"
end
But I can't find a way to express a many-to-many relationship (employee
to team is many-to-one).
I hope I'm a little more clear now. I'm pretty sure I'm not the first to
stumble on this kind of situation (multicriteria search with many to
many relation).
Thanks in advance.
Martin
-----Original Message-----
From: rails-bounces at lists.rubyonrails.org
[mailto:rails-bounces at lists.rubyonrails.org] On Behalf Of Victor
Jalencas
Sent: Tuesday, January 25, 2005 9:51 AM
To: rails at lists.rubyonrails.org
Subject: Re: [Rails] Query on many to many relation
Martin, you could do this:
company=Company.find(company_id)
company.employees
The last expression will return you the list of employees. All the hard
works is done by Rails behind the scenes
cheers
Victor
Van Aken Martin (GFDI) wrote:
> Hello all,
>
> I'm starting to develop a small application using rails, with great
> success (and pleasure) until now.
>
> I've stumbled on a very simple problem, but I can't find any
> documentation on this :
>
>
>
> I've have two classes with a "has_and_belong_to_many" relation (and so
> three tables). For simplicity, I will name them "employee" and
"company".
>
> I would like to make a find to retrieve all employees who are employed
> in a given company.
>
>
>
> Something like : Employee.find_all([company = ?],company_id)
>
>
>
> How can I reference the join table to make this work ?
>
> The SQL would be something like :
>
>
>
> Select Employees.* from Employees, Companies, Companies_Employees
where
>
> Companies.id =
> company_id //where
>
> AND Companies.id = Companies_Employees.Company_id //join
>
> AND Emploees.id = Companies_Employees.Employee_id //join
>
>
>
> But, if possible, I would like to avoid the "find_by_sql"
>
>
>
> Thanks for any help, and please forgive me for my English,
>
>
>
> Martin Van Aken
>
>
>
------------------------------------------------------------------------
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
_______________________________________________
Rails mailing list
Rails at lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
More information about the Rails
mailing list