Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has_many :through query with both values

Let's say I have:

class Post
  has_many :tags, :through => :taggings
  has_many :taggings
end

Notice there's no :include. Now say I want to retrieve all taggings and tags in the same query. How could I do that?

I'm looking for something like:

taggings = post.taggings(:include => tags) # doesn't work

I could make a custom query or add a third association to Post with an :include, but neither feels right.

like image 480
mahemoff Avatar asked Mar 16 '26 01:03

mahemoff


1 Answers

I think you can use includes on the association proxy as you would with the model class:

taggings = post.taggings.includes(:tag)
like image 86
rabusmar Avatar answered Mar 17 '26 14:03

rabusmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!