问题 has_many上的自定义范围:通过关联(Rails 4)


CONTEXT

在我的设置中,用户通过CommunityUser拥有许多社区,社区通过CommunityPost拥有许多帖子。如果遵循,则用户通过社区拥有许多帖子。

User.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社区的完整子集的帖子。

因此,给定一个community_ids为[1,2,3]的current_user,调用“current_user.posts”只会产生那些“community_ids”数组是其中一个的帖子 1,[2],[3],[1,2],[1,3],[2,3]或[1,2,3]。

我一直在研究范围 这里,但似乎无法确定如何成功实现这一目标......


8390
2018-05-17 22:25


起源



答案:


好问题......

我的直接想法:

-

ActiveRecord Association Extension

这些基本上允许您为关联创建一系列方法,允许您确定特定条件,如下所示:

#app/models/user.rb
has_many :communities, through: :community_users
has_many :posts, through: :communities do
   def in_community
       where("community_ids IN (?)", user.community_ids)
   end
end

-

条件协会

您可以在关联中使用条件,如下所示:

#app/models/user.rb
has_many :posts, -> { where("id IN (?)", user.community_ids) }, through: :communities #-> I believe the model object (I.E user) is available in the association

-

资源

我原本以为这是你最好的选择,但是更深入地看一下,我认为这只是你想要使用不同的联想名称

指定has_many:through使用的源关联名称   查询。只有在无法从中推断出名称时才使用它   协会。 has_many:订阅者,通过::订阅将会看起来   对于:订阅者或订阅者订阅者,除非a   :给出了来源。


诚实,这是需要一些思考的问题之一

首先,你如何存储/调用 community_ids 阵列?它是直接存储在db中,还是通过ActiveRecord方法访问 Model.association_ids

期待着进一步帮助您!


11
2018-05-18 08:19



嗨,为延迟道歉!我有点认为这个问题需要一段时间才能得到答案,因为它需要你提到的一些想法。是的,我正在使用Rails并使用ActiveRecord方法进行community_ids。 - neon
正如您所提到的,用户可以使用条件关联:“has_many:posts, - >(user){where(”id IN(?)“,user.community_ids)},通过:: community”,但在这种情况下不会'我还需要以某种方式检查每个帖子的社区吗?只是大声思考。 - neon
旁注:做“where(”id IN(?)“,user.community_ids)”这样会返回与“user.community_ids”中的任何一个相关联的帖子,而不是返回与“用户”完整子集的社区关联的帖子.community_ids” - neon
该 conditional association 你提到的应该工作! - Pavan
@Pavan你能提供一个例子吗? - neon


答案:


好问题......

我的直接想法:

-

ActiveRecord Association Extension

这些基本上允许您为关联创建一系列方法,允许您确定特定条件,如下所示:

#app/models/user.rb
has_many :communities, through: :community_users
has_many :posts, through: :communities do
   def in_community
       where("community_ids IN (?)", user.community_ids)
   end
end

-

条件协会

您可以在关联中使用条件,如下所示:

#app/models/user.rb
has_many :posts, -> { where("id IN (?)", user.community_ids) }, through: :communities #-> I believe the model object (I.E user) is available in the association

-

资源

我原本以为这是你最好的选择,但是更深入地看一下,我认为这只是你想要使用不同的联想名称

指定has_many:through使用的源关联名称   查询。只有在无法从中推断出名称时才使用它   协会。 has_many:订阅者,通过::订阅将会看起来   对于:订阅者或订阅者订阅者,除非a   :给出了来源。


诚实,这是需要一些思考的问题之一

首先,你如何存储/调用 community_ids 阵列?它是直接存储在db中,还是通过ActiveRecord方法访问 Model.association_ids

期待着进一步帮助您!


11
2018-05-18 08:19



嗨,为延迟道歉!我有点认为这个问题需要一段时间才能得到答案,因为它需要你提到的一些想法。是的,我正在使用Rails并使用ActiveRecord方法进行community_ids。 - neon
正如您所提到的,用户可以使用条件关联:“has_many:posts, - >(user){where(”id IN(?)“,user.community_ids)},通过:: community”,但在这种情况下不会'我还需要以某种方式检查每个帖子的社区吗?只是大声思考。 - neon
旁注:做“where(”id IN(?)“,user.community_ids)”这样会返回与“user.community_ids”中的任何一个相关联的帖子,而不是返回与“用户”完整子集的社区关联的帖子.community_ids” - neon
该 conditional association 你提到的应该工作! - Pavan
@Pavan你能提供一个例子吗? - neon


您没有显示for的模型和关系定义 Community 要么 CommunityPost 所以要确保你有一个 has_many :community_posts 和a has_many :posts, through: :community_posts 在你的 Community 模特和一个 belongs_to :community 和a belongs_to :post 上 CommunityPost。如果您不需要跟踪任何内容 ComunityPost  你可以用一个 has_and_belongs_to_many :posts 上 Community 和一个连接表 communities_posts 只包含外键 community_id 和 post_id

假设您按照我的描述设置了关系,那么您应该可以使用 current_user.posts 获得可以进一步链接的关系,并返回用户在呼叫时与之关联的所有社区的所有帖子 .all 在上面。定义你的Post模型的任何类方法(例如范围方法)也可以从该关系中调用,因此除非这些改进属于 Community 要么 CommunityPost  在这种情况下,你会把它们放在 Community 要么 CommunityPost 模型分别。实际上很少需要对范围进行AR关系扩展,因为通常您还希望能够独立于您可能用于获取它的任何相关模型来优化模型。


0
2018-05-24 04:29