sql - rails: Get all items tagged x AND y AND z -
I have two models: items
and tags
. The name of the two features is that I have to find tagged items with many tags.
class items & lt; ActiveRecord :: Base has_many: tags validates_presence_of: name end class tag & lt; Looking at a list of tag IDs, I can easily get a list of items with a tag or tag: ActiveRecord :: base is_to: item validates_presence_of: name end
Other:
# Tag_ ID Find all items tagged with one or more tags Item: All (: Terms = & gt; [tags.id in (? ), Tag_ids]: = & gt;: tag)
If tag_ids
, then {1,4}
, Then I get all the images tagged with 1, or 4, or both.
I now want to know how to get pictures - both are tagged with - and 4.
I can not even imagine SQL here.
You can group this result and check it out:
< Code> Items.All (: Terms => ['tag' IN '(?)', Tag_ids],: Johns =>: Tags, group => Items.id ',: having => ['COUNT (*)> =?', Tag_ids.length])
Comments
Post a Comment