Yeah, kinda dumb title, but I found it hard to describe my problem. I have the following tables:
properties
id | name
1 | color
2 | material
options
id | property_id | name
1 | 1 | yellow
2 | 1 | blue
3 | 2 | wood
4 | 2 | stone
substances
id | name
1 | orange juice
2 | cheese
relations
id | substance_id | option_id
1 | 2 | 1
2 | 2 | 3
3 | 1 | 1
Now, I have a list of options and want to know which substances are related to all those options. (For example, which substances are yellow and made of wood?) Is this possible with one query?
I’m trying to do this in Rails.
Or
Having a single
optionstable with theproperty_idto tell apart different kinds of options is not such a good idea and really makes this much harder. I’d suggest breaking out the different kinds of options into different tablesand using separate relations for each type of table. In this case, you wouldn’t need separate tables for each relation, since it appears that it’s a many (substance) to one (color) relation.
Then your query is much simpler
ActiveRecord should easily be able to handle this last query much more easily than the first two.