我正在尝试在以下关系中提取给定用户的所有帖子。不确定我是否正确,所以我会更好地解释。用户在某些组中拥有所有权和成员资格。用户可以是组的成员或所有者,但不能两者兼而有之。每个 Post 都有一个用户和组的 id。我认为问题是由于下面提到的关系。我怎样才能绕过它?还有一件事。我还必须找到用户组中其他用户发布的所有帖子。换句话说,我必须通过组。
/-- Owner ---\
User -- -- Group -- Post
| \-- Member --/ |
|_______________________________|
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
has_many :ownerships, :foreign_key => "user_id", :dependent => :destroy
has_many :memberships, :foreign_key => "user_id", :dependent => :destroy
# Problem with these two? I think so.
has_many :groups, :through => :memberships, :source => :user
has_many :groups, :through => :ownerships, :source => :user
class Ownership < ActiveRecord::Base
belongs_to :users, :class_name => "User"
belongs_to :groups, :class_name => "Group"
has_many :posts, :through => :groups, :source => :posts
class Membership < ActiveRecord::Base
belongs_to :users, :class_name => "User"
belongs_to :groups, :class_name => "Group"
has_many :posts, :through => :groups, :source => :posts
class Group < ActiveRecord::Base
has_many :posts, :dependent => :destroy
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :groups
错误来自以下行:
_groups = user.groups
错误如下:
找不到源关联:模型所有权中的用户。尝试 'has_many :groups, :through => :ownerships, :source => '。它是 :users、:groups 还是 :postings 之一?