Rails: Get all associated models from has-many association -
i need associated models other associated model, on want run query first.
for example, got post model , tag model. need posts, associated tags. there's no problem, if have 1 tag – call 'tag.posts', if have more, 1 tag – example, need somethink like:
post.where(id: posttag.where(tag_id: some_ids).pluck(:category_id).uniq)
i belive rails have built-in solution. so, knows it?
my thought is:
post.joins(:post_tags).where('post_tags.tag_id' => some_ids).uniq
you can make scope easier reuse. don't think there built-in method situation.
Comments
Post a Comment