如何管理多对多关系

时间:2022-10-04 10:54:26

I have two models Group and Person that I want to have a many-to-many relationship, but I'm unclear on how to manage the relationship itself. I want to be able to create groups and persons separately -- NOT necessarily via a nested model -- and then link persons to groups from the group view/model itself.

我有两个模型Group和Person,我想要有多对多的关系,但我不清楚如何管理这种关系本身。我希望能够分别创建组和人员 - 不一定是通过嵌套模型 - 然后将人员从组视图/模型本身链接到组。

Does anyone have any suggestions on how to do so?

有没有人对如何做有任何建议?

I thought of creating a many-to-many relationship via a join model and then accepting nested attributes for the join model in the Group model -- so I believe I will be able to add and remove relationships via the Group view/model. Does this approach make sense?

我想通过连接模型创建多对多关系,然后接受组模型中连接模型的嵌套属性 - 所以我相信我将能够通过组视图/模型添加和删除关系。这种方法有意义吗?

2 个解决方案

#1


1  

I would create a PersonGroup model that looks like this:

我会创建一个如下所示的PersonGroup模型:

class PersonGroup < ActiveRecord::Base
  has_many :people
  has_many :groups
end

And you might also do rails generate migration create_person_group and put this in the up method of the generated migration file:

您也可以执行rails generate migration create_person_group并将其放在生成的迁移文件的up方法中:

create_table :person_group do |t| 
  t.integer :person_id, :null => false
  t.integer :group_id, :null => false

  t.timestamps
end 

add_index :person_group, [:person_id, :group_id], :unique => true

Then in Person:

然后在人:

class Person < ActiveRecord::Base
  has_many :person_groups
  has_many :groups, :through => :person_groups
end

And in Group:

在集团:

class Group < ActiveRecord::Base
  has_many :person_groups
  has_many :people, :through => :person_groups
end

#2


0  

Create a junction table. Junction tables are used when you want to store many-to-many relationships. I don't develop in ROR so I don't know the specifics for ActiveRecord but I am sure that this can help you think about the problem as well.

创建联结表。当您想要存储多对多关系时,使用连接表。我没有在ROR中开发,所以我不知道ActiveRecord的具体细节,但我相信这也可以帮助你思考这个问题。

group_id INTEGER,
person_id INTEGER

#1


1  

I would create a PersonGroup model that looks like this:

我会创建一个如下所示的PersonGroup模型:

class PersonGroup < ActiveRecord::Base
  has_many :people
  has_many :groups
end

And you might also do rails generate migration create_person_group and put this in the up method of the generated migration file:

您也可以执行rails generate migration create_person_group并将其放在生成的迁移文件的up方法中:

create_table :person_group do |t| 
  t.integer :person_id, :null => false
  t.integer :group_id, :null => false

  t.timestamps
end 

add_index :person_group, [:person_id, :group_id], :unique => true

Then in Person:

然后在人:

class Person < ActiveRecord::Base
  has_many :person_groups
  has_many :groups, :through => :person_groups
end

And in Group:

在集团:

class Group < ActiveRecord::Base
  has_many :person_groups
  has_many :people, :through => :person_groups
end

#2


0  

Create a junction table. Junction tables are used when you want to store many-to-many relationships. I don't develop in ROR so I don't know the specifics for ActiveRecord but I am sure that this can help you think about the problem as well.

创建联结表。当您想要存储多对多关系时,使用连接表。我没有在ROR中开发,所以我不知道ActiveRecord的具体细节,但我相信这也可以帮助你思考这个问题。

group_id INTEGER,
person_id INTEGER