让三个不同父母的子女进行多态关联

时间:2016-03-05 18:14:03

标签: ruby-on-rails activerecord polymorphism rails-activerecord ruby-on-rails-5

我有模型Post,通过多态性属于OrganizationTeamUser

对于每位家长,我都会像这样获取帖子:

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 个答案:

答案 0 :(得分:2)

这应该可以解决问题:

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