(Crossposting note: Since I didn’t get any response here, I posted this

also on

http://stackoverflow.com/questions/24607428/rubyonrails-counting-the-result-of-a-join)

I have in my model :dicts, :cards and :idioms. Each Dict has many Cards

and each Cards has many Idioms. Also, :idioms has an Integer column

:kind.

I would like to find out, whether a certain dict object has at least

one Card which has at least one Idiom where :kind has a certain value.

This is my (working) code:

def has_kind?(dict,kind)

Card. joins(:idioms). where(“dict_id=#{dict.id} and

cards.id=idioms.card_id and kind=#{kind}”). count > 0

end

This works, but can it be done better? I also tried to omit at least one

of the id comparision by doing something like:

dict.cards.where(“cards.id = …”).count > 0

but this doesn’t work (“count” is not applicable in this case).