Rails conditional request

Suppose we have two tables: "Items" and "Types". This relationship:

item belongs_to type
type has_many items

      

Also, there is a column in the Item table, name it "mark". What would the request be (in a Rails safe way, if possible) to extract all types from it. A table of types that have related items in the Items table with "tag"?

+2


source to share


1 answer


It:

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

      



must work.

Note. You should not use it Type

as a class name and not :type

as an attribute, as this name can lead to conflicts.

+5


source







All Articles