views:

66

answers:

1

Assume, we have two tables: "Items" and "Types". The relationships are:

item belongs_to type
type has_many items

Also, the Item table have a column, let's call it "mark". What would be a query (in a Rails secure way if it's possible) to extract all the types from them Types table, wich have connected items in Items table with a "mark"?

+5  A: 

This:

Type.find :all, :include => items, :conditions => ['items.mark = ?', somevalue]

should work.

Note: you shouldn't use Type as class name, nor :type as attribute, as this name can lead to conflicts.

giorgian
Thanks! You unclosed :include for me! The Type-like names were for example only
gmile