1

我有模型Post,通过多态性属于Organization,TeamUser获取三个不同父母的多态性关联儿童

对于每个父我取这样的帖子:

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

我怎么会合并这些查询返回的所有帖子一个AR对象?

我曾尝试以下:

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

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

你有没有尝试过这样的:'Post.where(可跟踪:[CURRENT_USER,current_user.organization, current_user.team])'? – jvillian

+0

它的工作原理!而且我现在发现我也可以使用Rails 5 new。或像'Post.where(trackable:current_user).or(Post.where(trackable:current_user.team))''。但是按照你的建议将它们放入一个数组中会更方便。谢谢! –

+0

太棒了。很高兴它的工作。我发布它作为答案,所以你可以接受。 – jvillian

回答

1

这应该做的伎俩:

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