【发布时间】:2014-07-06 04:04:05
【问题描述】:
上下文:
在我的设置中,用户通过 CommunityUser 拥有许多社区,而社区通过 CommunityPost 拥有许多帖子。如果遵循然后,该用户通过社区有很多帖子。
用户.rb
has_many :community_users
has_many :communities, through: :community_users
has_many :posts, through: :communities
鉴于上述 User.rb,调用“current_user.posts”会返回包含一个或多个与 current_user 相同的社区的帖子。
问题:
我希望改进该关联,以便调用“current_user.posts”仅返回其社区是 current_user 社区的完整子集的那些帖子。
因此,给定一个 current_user 的 community_ids 为 [1,2,3],调用“current_user.posts”只会产生那些“community_ids”数组为1、[2]、[3]、[ 1,2]、[1,3]、[2,3] 或 [1,2,3]。
我一直在研究范围 here,但似乎无法确定如何成功完成此任务...
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord scope associations