在 Ruby on Rails 3 中连接 3 个表

发布于 2024-12-04 11:58:49 字数 1531 浏览 0 评论 0原文

我试图在以下关系中提取给定用户的所有帖子。不确定我是否正确,所以我会更好地解释。用户在某些组中拥有所有权和成员资格。用户可以是组的成员或所有者,但不能同时是两者。每个帖子都有一个用户和组的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-12-11 11:58:49

首先:您之所以会看到这个错误,是因为您在 MembershipOwnership 表中定义了关联,如下所示:

belongs_to :users

当它们应该只属于一个时user,即单数user

belongs_to :user

但即使这样你也会遇到问题!

我认为拥有 Membership 模型和 Ownership 模型将是接下来让您陷入困境的原因。我不明白拥有 Ownership 模型的目的是什么,除了表示组的所有权之外,这可以通过 memberships 表记录中名为 的字段来完成例如所有者。这是过度设计。

Rails 代码的问题在于,您定义了通过一个关联有许多帖子,然后告诉它您通过另一个关联有很多帖子。实际上,您正在这样做:

 def posts
   # find posts for the groups that I own
 end

 def posts
   # find posts for the groups I belong to
 end

这里有两个同名的方法并不是错误的。这正是您通过定义两个同名的 has_many 关联所做的事情。

所以希望现在您可以明白为什么拥有 OwnershipMembership 模型是通往 疯狂

我真的建议您只拥有一个 Membership 模型,该模型具有声明组所有者的布尔属性。这也意味着,如果您愿意,您可以通过一种非常简单的方式为一个组添加新的所有者:只需翻转布尔值即可。无需在另一个表中创建另一个记录。

一种会员制模型来统治它们。

First up: you're getting that error you're seeing because you've defined the associations in the Membership and Ownership table as this:

belongs_to :users

When they should belong to only one user, i.e. singular user:

belongs_to :user

But even then you will run into problems!

I think having a Membership model and an Ownership model are what will trip you up next. I don't understand what the purpose of having an Ownership model provides, other than signifying ownership of a group, which could be done by a field on the memberships table's records called owner 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:

 def posts
   # find posts for the groups that I own
 end

 def posts
   # find posts for the groups I belong to
 end

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 a Membership 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文