Ruby-on-rails Rails (ActiveRecord) 多对多表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:30:24  来源:igfitidea点击:

Rails (ActiveRecord) many to many table

ruby-on-railsrubyactiverecordmany-to-many

提问by Ori

I have two models, Users and Groups. Each group can have many users and each user can be in many groups.

我有两个模型,用户和组。每个组可以有多个用户,每个用户可以在多个组中。

I currently have something simple like:

我目前有一些简单的东西:

User:

用户:

has_many    :groups

Group:

团体:

has_many    :users

So I have a groups_users table which is just creating rows with group_id and user_id. I want to add another column to this, (which I have), the question is how do I access it from a model without using a custom SQL call? In the group model I can go self.users and in user I can go self.groups

所以我有一个groups_users 表,它只是用group_id 和user_id 创建行。我想为此添加另一列(我有),问题是如何在不使用自定义 SQL 调用的情况下从模型访问它?在组模型中我可以去 self.users 而在用户中我可以去 self.groups

Is there a way to change the third column in this table from a user model?

有没有办法从用户模型更改此表中的第三列?

Sorry if this is confusing, please advise on this

对不起,如果这令人困惑,请就此提出建议

回答by dave elkins

Here are a couple of tutorials that should help. Basically there two approaches to make many-to-many work, either has_and_belongs_to_many or has_many :through (recommended).

这里有几个教程应该会有所帮助。基本上有两种方法可以进行多对多工作,has_and_belongs_to_many 或 has_many :through(推荐)。

links:

链接:

  1. http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
  2. http://railscasts.com/episodes/47-two-many-to-many
  3. http://railscasts.com/episodes/154-polymorphic-association
  1. http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
  2. http://railscasts.com/episodes/47-two-many-to-many
  3. http://railscasts.com/episodes/154-polymorphic-association

回答by bigpotato

In Rails 3 you want to make a join table for many to many relationships, using the plural names of the tables you want to join in alphabetical order. So in this case it would be groups_users.

在 Rails 3 中,您想为多对多关系创建一个连接表,使用要按字母顺序连接的表的复数名称。所以在这种情况下它会是groups_users.

models

楷模

class GroupsUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :groups_users
  has_many :groups, :through => :groups_users
end

class Group < ActiveRecord::Base
  has_many :groups_users
  has_many :users, :through => :groups_users
end

回答by Ethan

I [added] another column to [users_groups]...The question is how do I access it from a model without using a custom SQL call?

我 [添加] 另一列到 [ users_groups]...问题是如何在不使用自定义 SQL 调用的情况下从模型访问它?

It sounds like you want to access a column of your user_groupstable by calling a method on your Usermodel or your Groupmodel.

听起来您想user_groups通过调用User模型或Group模型上的方法来访问表的列。

Some suggestions:

一些建议:

I'd name the table "user_groups" to work with ActiveRecord's pluralization expectations, but I'm not sure if that's essential.

我将表命名为 " user_groups" 以符合 ActiveRecord 的复数期望,但我不确定这是否必要。

Following Dave's advice, you'd want to set things up using the "has_many :through" technique...

按照 Dave 的建议,您可能希望使用“ has_many :through”技术进行设置...

# Declare a Model based on the many-to-many linking table.
class UserGroup < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :user_groups
  has_many :groups, :through => :user_groups
end

class Group < ActiveRecord::Base
  has_many :user_groups
  has_many :users, :through => :user_groups
end

Is there a way to change the third column in this table from a user model?

有没有办法从用户模型更改此表中的第三列?

This is a little unclear, but keep in mind that each Usercan have a lot of UserGroups. So if you wanted to change that third column you'd have to find the particular one you're looking for.

这有点不清楚,但请记住,每个都User可以有很多UserGroups. 因此,如果您想更改第三列,则必须找到您要查找的特定列。