WhoDidThis
WhoDidThis

Reputation: 383

Merge associated items from an activerecord relation result

How do I combine for example all the Tag.has_many :images, through: :taggings from a bunch of Tags?

Something around this unless theres something simpler:

images = ???
Tag.where("name in (?)", tag_params).each do |tag|
  images = images.merge(tag.images)
end

Upvotes: 0

Views: 47

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

How about:

images = Image.joins(:tags).where(['tags.id in (?)', tag_params])

?

Upvotes: 1

Related Questions