Rails find by *all* associated tags.id in

Posted by mark on Stack Overflow See other posts from Stack Overflow or by mark
Published on 2010-04-02T16:39:06Z Indexed on 2010/04/02 16:43 UTC
Read the original article Hit count: 182

Filed under:
|
|
|

Hi

Say I have a model Taggable has_many tags, how may I find all taggables by their associated tag's taggable_id field?

Taggable.find(:all, :joins => :tags, :conditions => {:tags => {:taggable_id => [1,2,3]}})

results in this:

SELECT `taggables`.* FROM `taggables` INNER JOIN `tags` ON tags.taggable_id = taggables.id WHERE (`tag`.`taggable_id` IN (1,2,3))

The syntax is incredible but does not fit my needs in that the resulting sql returns any taggable that has any, some or all of the tags.

How can I find taggables with related tags of field taggable_id valued 1, 2 and 3?

Thanks for any advice. :)

© Stack Overflow or respective owner

Related posts about rails

Related posts about activerecord