Reputation: 181
I am trying to preload an association, say you have Posts
and subscribers
.
I want to preload all posts where the current user is a subscriber
, but at the same time, preload all the subscribers
as well.
The query I am using is like this:
Post.includes(:subscribers)
.references(:subscribers)
.where(subscribers: { id: current_user.id })
This gives me the correct list of posts
but does not give me all the subscribers
for each post
, just the current user. Is there a way to get all the subscribers
while at the same time scope it to only posts that I can see as a subscriber?
Upvotes: 0
Views: 781
Reputation: 665
Try this
Post.includes(:subscribers).where(id: Post.joins(:subscribers).where(subscribers: {id: current_user.id}).select(:id))
Upvotes: 2