Fellow Stranger
Fellow Stranger

Reputation: 34013

Get children of three different parents in polymorphic association

I have the model Post, that via polymorphism belongs to either Organization, Team or User.

For each parent I'm fetching the posts like this:

current_user.posts
current_user.organization.posts
current_user.team.posts

How would I merge these queries returning a single AR object with all posts?

I have tried the following:

Post.where(trackable: current_user).where(trackable: current_user.team) # returns 0 objects

current_user.posts + current_user.organization.posts # this returns an array

Upvotes: 1

Views: 313

Answers (1)

jvillian
jvillian

Reputation: 20263

This should do the trick:

Post.where(trackable: [current_user, current_user.organization, current_user.team])

Upvotes: 2

Related Questions