I have 2 models:
class Item < ApplicationRecord
has_many :tags, dependent: :destroy
end
class Tag < ApplicationRecord
belongs_to :item
end
My current query will return Items that have ANY of the tags in a list, I need it to only return Items that have ALL of the tags.
tag_list = ['Foo', 'Bar', 'Baz'] # may contain more
Item.distinct
.joins(:tags)
.where(tags: { name: tag_list })
What can I do to return only the Items that have ALL the Tag names from my list using ActiveRecord?
Thanks for the help.
Edit: I’m using postgres
3
Answers
You can filter all the tags having name ‘Bar’, ‘Baz’, ‘Foo’ (order matters) and group them by their item_id, then you can get the items with those ids;
Here the
ARRAY[?]::VARCHAR[]
is because tags.name is of type character varying, so you have to map the value to the right of the=
inHAVING
to whatever is the output ofARRAY_AGG(DISTINCT name ORDER BY name)
.This should work:
Can we use
where("tags.name in (?)", tag_list)
?(If anyone has any suggestions, I would appreciate it)