让三个不同父母的孩子处于多态关联状态



我有一个模型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

这应该可以解决问题:

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

最新更新