Ruby On Rails关系 - 一对多

时间:2021-12-18 09:54:41

I'm a beginning to ROR, but here's what I'm trying to achieve. I have two items I want to associate: matters and people. Each matter can have many people. That is, I want to create people and matters separately and later be able to link them.

我是ROR的开始,但这就是我想要实现的目标。我有两件我想要联想的事:事和人。每件事都可以有很多人。也就是说,我想分别创建人和事,然后能够链接它们。

For example, I may create: Bill Clinton Barack Obama

例如,我可能会创造:比尔克林顿巴拉克奥巴马

I may create the matters: Global warming War on terror

我可能会创造一些问题:全球变暖反恐战争

I want to be able to associate the users Bill Clinton AND Barack Obama to BOTH matters. Can someone point me to a tutorial that can show me how to do this?

我希望能够将用户比尔克林顿和巴拉克奥巴马联系起来。有人能指点我的教程,可以告诉我如何做到这一点?

3 个解决方案

#1


I think has_and_belongs_to_many is used less and less by the RoR community now. While still supported, I think it is now more common to have an intermediate model (in your case something like PoliticianMatter) to join your Politician and Matter models.

我认为现在,RoR社区越来越少地使用has_and_belongs_to_many。虽然仍然支持,但我认为现在更常见的是有一个中间模型(在你的情况下像PoliticianMatter)加入你的Politician和Matter模型。

Then your politician_matter table will have a PK, a politician_id and a matter_id.

然后你的politician_matter表将有一个PK,一个politician_id和一个matter_id。

Then you have

那你有

class PoliticanMatter < ActiveRecord::Base
  belongs_to :politician
  belongs_to :matter
end

The advantage of this approach is that if there ever need to be future properties of the politician -> matter relationship (e.g importance, date of last occurrence) you have a model which affords this - has_and_belongs_to_many would not support the addition of these extra properties.

这种方法的优点是,如果需要政治家的未来属性 - >物质关系(例如重要性,最后一次出现的日期),你有一个提供这个的模型 - has_and_belongs_to_many不支持添加这些额外的属性。

You can also access the many to many relationship directly from the Politician and Matter models like this

您还可以直接从Politician和Matter模型中访问多对多关系

class Politician < ActiveRecord::Base
  has_many :politician_matters
  has_many :matters, :through => :politician_matters
end

class Matter < ActiveRecord::Base
  has_many :politician_matters
  has_many :politicians, :through => :politician_matters
end

#2


You need a many2many relationship between these two entities.

你需要在这两个实体之间建立多种关系。

  • A matter can be studied by many people
  • 很多人都可以研究一件事

  • A person can studie several matters
  • 一个人可以研究几个问题

Rails uses the has_and_belongs_to_many helper to do that. You'll find more about that in the documentation and many many blog posts!

Rails使用has_and_belongs_to_many帮助器来做到这一点。您可以在文档和许多博客文章中找到更多相关内容!

has_and_belongs_to_many helper

#3


class Politician < ActiveRecord::Base
  has_and_belongs_to_many :tasks
end

class Task < ActiveRecord::Base
  has_and_belongs_to_many :politicians
end

What you need are 3 tables: politicians, tasks and politicians_tasks (having the two columns politician_id and task_id, no primary key)

你需要的是3个表:政治家,任务和政治家_任务(有两列politician_id和task_id,没有主键)

Hope this helps Seb

希望这有助于Seb

#1


I think has_and_belongs_to_many is used less and less by the RoR community now. While still supported, I think it is now more common to have an intermediate model (in your case something like PoliticianMatter) to join your Politician and Matter models.

我认为现在,RoR社区越来越少地使用has_and_belongs_to_many。虽然仍然支持,但我认为现在更常见的是有一个中间模型(在你的情况下像PoliticianMatter)加入你的Politician和Matter模型。

Then your politician_matter table will have a PK, a politician_id and a matter_id.

然后你的politician_matter表将有一个PK,一个politician_id和一个matter_id。

Then you have

那你有

class PoliticanMatter < ActiveRecord::Base
  belongs_to :politician
  belongs_to :matter
end

The advantage of this approach is that if there ever need to be future properties of the politician -> matter relationship (e.g importance, date of last occurrence) you have a model which affords this - has_and_belongs_to_many would not support the addition of these extra properties.

这种方法的优点是,如果需要政治家的未来属性 - >物质关系(例如重要性,最后一次出现的日期),你有一个提供这个的模型 - has_and_belongs_to_many不支持添加这些额外的属性。

You can also access the many to many relationship directly from the Politician and Matter models like this

您还可以直接从Politician和Matter模型中访问多对多关系

class Politician < ActiveRecord::Base
  has_many :politician_matters
  has_many :matters, :through => :politician_matters
end

class Matter < ActiveRecord::Base
  has_many :politician_matters
  has_many :politicians, :through => :politician_matters
end

#2


You need a many2many relationship between these two entities.

你需要在这两个实体之间建立多种关系。

  • A matter can be studied by many people
  • 很多人都可以研究一件事

  • A person can studie several matters
  • 一个人可以研究几个问题

Rails uses the has_and_belongs_to_many helper to do that. You'll find more about that in the documentation and many many blog posts!

Rails使用has_and_belongs_to_many帮助器来做到这一点。您可以在文档和许多博客文章中找到更多相关内容!

has_and_belongs_to_many helper

#3


class Politician < ActiveRecord::Base
  has_and_belongs_to_many :tasks
end

class Task < ActiveRecord::Base
  has_and_belongs_to_many :politicians
end

What you need are 3 tables: politicians, tasks and politicians_tasks (having the two columns politician_id and task_id, no primary key)

你需要的是3个表:政治家,任务和政治家_任务(有两列politician_id和task_id,没有主键)

Hope this helps Seb

希望这有助于Seb