Rails Active Record 可以同时理解两个表之间的两个同时关系吗?

发布于 2024-08-19 05:20:53 字数 787 浏览 6 评论 0原文

我有两个表,用户。一个用户可以属于多个组。一个组可以有许多用户。

因此,我使用连接表 groups_users 在用户和组之间创建了 have_and_belongs_to_many 关系。这一切都按预期进行。

我还想做的是为每个用户指定一个活动组。如果不是我已经定义的 habtm 关系,我会在用户中为活动组创建一个列“group_id”,然后我会在模型之间定义一对多关系,如下所示

class User < ActiveRecord::Base
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :users
end

: 。我无法访问“@user.group.name”等组属性。我怀疑我通过指定两种关系对 Rails 要求太多。

所以我有三个问题。

  1. 如果组合这两种关系会混淆 Active Record ,我可以很容易地理解。是这样吗?
  2. 如果是这样,您将如何实现这些关系?现在我只是手动使用 group_id,但这感觉很混乱。
  3. 无论我是使用 Active Record 魔法还是手动设置活动组,用户的活动组都可能位于使用第一个 habtm 关系所属的组之外。关于如何在活动组必须是用户所属组的约束下实现这一点,有什么想法吗?

感谢您的任何见解。我已经进入 Rails 学习曲线几周了,我认为深入了解这个小问题将加深我对模型和表关系的理解。

I have two tables, users and groups. A user can belong to many groups. A group can have many users.

So I have created a have_and_belongs_to_many relationship between users and groups using a join table, groups_users. This all works as expected.

What I would also like to do is specify an ACTIVE group for each user. Were it not for the habtm relationship I have already defined, I would create a column “group_id” in users for the active group, and then I would define a one-to-many relationship between the models as follows:

class User < ActiveRecord::Base
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :users
end

This didn’t work. I could not access group properties like “@user.group.name”. I suspect that I’m asking too much of Rails by specifying two relationships.

So I have three questions.

  1. I could very easily understand if combining the two relationships confuses Active Record . Is that the case?
  2. If so, how would you implement these relationships? Right now I’m just manually using the group_id, but that feels messy.
  3. Regardless of whether I am using Active Record magic or manually setting the active group, it is possible for a user’s active group to be outside the group’s they belong to using the first habtm relationship. Any thoughts on how to implement this with the constraint that the active group must be a group that the user belongs to?

Thanks for any insights. I am a couple of weeks into the Rails learning curve and I think that getting to the bottom of this little problem will deepen my understanding of models and table relationships quite a bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

裂开嘴轻声笑有多痛 2024-08-26 05:20:53

格雷格 - 我以前做过这个,类似于你的提议。我使用“primary_group”而不是“group”,并将foreign_key设置为“primary_group_id”,并将 :class_name => “组”

我不经常使用 has_and_belongs_to_many 所以我不知道这是否会导致问题,但我不这么认为 - 至少在用户模型中不会。你没有#group方法,所以设置belongs_to应该没问题。我不知道您是否绝对必须在组上有一个 has_many :users 方法,但无论如何您可能不需要它。这肯定会导致与 habtm 创建的“users”方法发生冲突。如果您确实需要这个,请尝试 has_many :primary_users 并设置 :foreign_key => :primary_group_id

只是一些想法。您应该能够做您现在正在做的事情,所以不确定是什么失败了。

Greg - I've done this before, similar to what as your proposing. I used a "primary_group" instead of "group" and set the foreign_key to be "primary_group_id" and the :class_name => 'Group'

I don't use has_and_belongs_to_many very often so I don't know if that would be causing an issue, but I don't think so - at least not with the user model. You don't have a #group method, so setting belongs_to should be OK. I don't know if you absolutely must have have a has_many :users method on the group, but you may not need it anyway. This will certainly cause a collision with the 'users' method that habtm creates. If you do need this, try has_many :primary_users and set the :foreign_key => :primary_group_id

Just some thoughts. You should be able to do what you're doing now, so not sure what's failing.

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