在 Ruby on Rails 3 中连接 3 个表
我试图在以下关系中提取给定用户的所有帖子。不确定我是否正确,所以我会更好地解释。用户在某些组中拥有所有权和成员资格。用户可以是组的成员或所有者,但不能同时是两者。每个帖子都有一个用户和组的 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 => :所有权,:来源=> '。它是 :users、:groups 或 :postings 之一吗?
I am trying to extract all Posts for a given User in the bellow relationships. Not sure whether I got them right, so I'll better explain. A User has Ownerships and Memberships in some Groups. A User can be either a Member or an Owner of the Group, but not both. Every Post has an id of the user and of the group. I think the problem is due to the relationships noted below. How can I get around it? One more thing. I have to also find all posts that were posted by other users in the user's groups. In other words, I have to pass through groups.
/-- 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
The errors is coming from the line:
_groups = user.groups
The error as following:
Could not find the source association(s) :user in model Ownership. Try 'has_many :groups, :through => :ownerships, :source => '. Is it one of :users, :groups, or :postings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:您之所以会看到这个错误,是因为您在
Membership
和Ownership
表中定义了关联,如下所示:当它们应该只属于一个时user,即单数
user
:但即使这样你也会遇到问题!
我认为拥有
Membership
模型和Ownership
模型将是接下来让您陷入困境的原因。我不明白拥有Ownership
模型的目的是什么,除了表示组的所有权之外,这可以通过memberships
表记录中名为 的字段来完成例如所有者
。这是过度设计。Rails 代码的问题在于,您定义了通过一个关联有许多帖子,然后告诉它您通过另一个关联有很多帖子。实际上,您正在这样做:
这里有两个同名的方法并不是错误的。这正是您通过定义两个同名的 has_many 关联所做的事情。
所以希望现在您可以明白为什么拥有
Ownership
和Membership
模型是通往 疯狂。我真的建议您只拥有一个
Membership
模型,该模型具有声明组所有者的布尔属性。这也意味着,如果您愿意,您可以通过一种非常简单的方式为一个组添加新的所有者:只需翻转布尔值即可。无需在另一个表中创建另一个记录。一种
会员制
模型来统治它们。First up: you're getting that error you're seeing because you've defined the associations in the
Membership
andOwnership
table as this:When they should belong to only one user, i.e. singular
user
:But even then you will run into problems!
I think having a
Membership
model and anOwnership
model are what will trip you up next. I don't understand what the purpose of having anOwnership
model provides, other than signifying ownership of a group, which could be done by a field on thememberships
table's records calledowner
for instance. It's over-engineering.The problem with the Rails code you've got there is that you're defining that you have many posts through one association and then you're telling it that you have many posts through another association. In effect, you're doing this:
It is not mistake here that there are two identically-named methods. This is exactly what you are doing by defining two
has_many
associations with the same name.So hopefully now you can see why having an
Ownership
and aMembership
model is the path to madness.I would really recommend that you just have a
Membership
model that has a boolean attribute declaring an owner for a group. This would also mean that, if you wanted to, you could have new owners for a group in a very easy fashion: just flip the boolean. No need to create another record in another table.One
Membership
model to rule them all.