[Rails] Query on many to many relation
Van Aken Martin (GFDI)
Martin.VanAken at gfdi.be
Tue Jan 25 08:26:17 GMT 2005
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://one.textdrive.com/pipermail/rails/attachments/20050125/04108407/attachment.html
More information about the Rails
mailing list