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.
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?
What you need are 3 tables:
politicians, tasks
andpoliticians_tasks
(having the two columnspolitician_id
andtask_id
, no primary key)Hope this helps Seb
You need a many2many relationship between these two entities.
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!has_and_belongs_to_many helper
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 likePoliticianMatter
) to join yourPolitician
andMatter
models.Then your
politician_matter
table will have a PK, apolitician_id
and amatter_id
.Then you have
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.You can also access the many to many relationship directly from the Politician and Matter models like this