Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我如何收集我所属小组成员拥有的所有主题?_Ruby On Rails_Model Associations_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 我如何收集我所属小组成员拥有的所有主题?

Ruby on rails 我如何收集我所属小组成员拥有的所有主题?,ruby-on-rails,model-associations,ruby-on-rails-4,Ruby On Rails,Model Associations,Ruby On Rails 4,这对我来说可能很难解释,所以如果不清楚,请告诉我,以便我可以根据需要进行编辑 我举了以下例子: class User < ActiveRecord::Base has_many :topics has_many :memberships end class Topic < ActiveRecord::Base belongs_to :user end #join model between User and Group class Membership < Ac

这对我来说可能很难解释,所以如果不清楚,请告诉我,以便我可以根据需要进行编辑

我举了以下例子:

class User < ActiveRecord::Base
  has_many :topics
  has_many :memberships
end

class Topic < ActiveRecord::Base
  belongs_to :user
end

#join model between User and Group
class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :topics, :through => :members
end
class用户:成员身份,:来源=>:用户
拥有多个:主题,:至=>:成员
结束
我遇到的问题是,我正在尝试创建一个feed(
@feed\u topics
),其中包含我所属组的所有成员所拥有的所有主题,这让我有点发疯

我应该尝试使用关联来实现这一点,还是在我的用户模型中创建一个实例方法,使用一些ActiveRecord/SQL将所有组的成员主题合并到一个ActiveRecord::Relation对象中


我的目标是在我的控制器的操作中编写
当前用户.feed\u主题。

很抱歉没有提前解释!我们的想法是利用“嵌套多个”来访问提要主题。这一概念记录在“嵌套关联”标题下:。 如果仍然不清楚(或者不起作用),请告诉我

class用户:成员资格
拥有多个:组成员,:至=>:组,:源=>:成员
有很多:提要主题,:至=>:组成员,:源=>:主题
结束

到目前为止,这些是原始问题中模型的最终版本(主题和成员身份没有改变):

class用户:成员身份
有很多:提要主题,:至=>:组,:源=>:成员主题
结束
类组:成员身份,:来源=>:用户
有很多:成员主题,:至=>:members,:source=>:topics
结束
我现在正在通过添加更多的组和成员来进行测试,看看它是否能吸引其他组中所有其他成员的主题

编辑:事情似乎进展顺利


EDIT2:我遇到的一个小问题是看到重复的主题,因为一个成员在多个组中。我了解了
:uniq=>true
,它挽救了这一天。

嗨,你能澄清一下GroupUser应该代表什么吗?一个用户有很多GroupUsers意味着什么?嗨@Rebizele,我编辑了我的问题来澄清。当我在这里时,GroupUser基本上是一个成员对象,将不同的用户与不同的组连接在一起。你现在可以自己回答你的第二个问题:)希望这有帮助。谢谢你的关注。我不清楚你在那里想做什么。我想我需要更多的解释。我将调整GroupUsers模型,现在将其称为Memberships,以使其更简单。希望目前没有人在寻找答案!我知道我有点困惑了。我想知道为什么在用户模型中有
有很多:组成员,:通过=>:groups,:source=>:member
,但我不怪你!也许这两种方法都能奏效。为了理解,我把它放在b/c组模型中,我觉得做
@Group.members
是有意义的。
class User < ActiveRecord::Base
  has_many :topics
  has_many :memberships
  has_many :groups, :through => :membership
  has_many :group_members, :through => :groups, :source => :member
  has_many :feed_topics, :through => :group_members, :source => :topic
end
class User < ActiveRecord::Base
  has_many :topics
  has_many :memberships
  has_many :groups, :through => :memberships
  has_many :feed_topics, :through => :groups, :source => :member_topics
end

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :member_topics, :through => :members, :source => :topics
end