Ruby on Rails Wednesday, July 2, 2014

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).

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d91bf7a05f29d5fb78cef7d4d6ea0f0d%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment