Rails (ActiveRecord)很多到很多表

时间:2021-07-02 09:57:22

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。用户和用户可以选择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

对不起,如果这让你感到困惑,请提出建议

3 个解决方案

#1


14  

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://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
  3. http://railscasts.com/episodes/47-two-many-to-many
  4. http://railscasts.com/episodes/47-two-many-to-many
  5. http://railscasts.com/episodes/154-polymorphic-association
  6. http://railscasts.com/episodes/154-polymorphic-association

#2


7  

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

#3


2  

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_groups table by calling a method on your User model or your Group model.

您似乎希望通过调用用户模型或组模型上的方法来访问user_groups表的列。

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 User can 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.

这有点不清楚,但是请记住每个用户都可以有很多用户组。如果你想改变第三列,你必须找到你要找的那个。

#1


14  

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://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off
  3. http://railscasts.com/episodes/47-two-many-to-many
  4. http://railscasts.com/episodes/47-two-many-to-many
  5. http://railscasts.com/episodes/154-polymorphic-association
  6. http://railscasts.com/episodes/154-polymorphic-association

#2


7  

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

#3


2  

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_groups table by calling a method on your User model or your Group model.

您似乎希望通过调用用户模型或组模型上的方法来访问user_groups表的列。

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 User can 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.

这有点不清楚,但是请记住每个用户都可以有很多用户组。如果你想改变第三列,你必须找到你要找的那个。