Rails Active Record 可以同时理解两个表之间的两个同时关系吗?
我有两个表,用户和组。一个用户可以属于多个组。一个组可以有许多用户。
因此,我使用连接表 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 要求太多。
所以我有三个问题。
- 如果组合这两种关系会混淆 Active Record ,我可以很容易地理解。是这样吗?
- 如果是这样,您将如何实现这些关系?现在我只是手动使用 group_id,但这感觉很混乱。
- 无论我是使用 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.
- I could very easily understand if combining the two relationships confuses Active Record . Is that the case?
- If so, how would you implement these relationships? Right now I’m just manually using the group_id, but that feels messy.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
格雷格 - 我以前做过这个,类似于你的提议。我使用“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.