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])