Forum: Ruby on Rails Joining tables and query design advice

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A4a3266a04bd8fb9935fa5684e14d6ef?d=identicon&s=25 Dustin Frisell (dustin)
on 2007-05-01 04:57
Hi everyone,

My problem involves two tables, Objects and Attributes:


|Object_ID   (foreign key references

I want to be able to search for Objects by (potentially many)
combinations of Attribute values.  To show all of the unique Objects
that have Attributes 'green' AND 'colorless', I am currently writing
this mess of code:

FROM attributes t1, attributes t2, objects
WHERE t1.val = 'green'
AND t2.val = 'colorless'
AND t1.object_id = t2.object_id
AND t1.object_id =

Is there a way I can search make queries like ('Show me all of the
records that have ALL of these attributes') and just feed it a list of

The technique I am using requires that I make a new table alias for each
new attribute.  Does rails have some built-in join mechanism to do
queries like this?

Thanks in advance,
This topic is locked and can not be replied to.