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?
-
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_manyhelper to do that. You'll find more about that in the documentation and many many blog posts! -
class Politician < ActiveRecord::Base has_and_belongs_to_many :tasks end class Task < ActiveRecord::Base has_and_belongs_to_many :politicians endWhat you need are 3 tables:
politicians, tasksandpoliticians_tasks(having the two columnspolitician_idandtask_id, no primary key)Hope this helps Seb
-
I think
has_and_belongs_to_manyis 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 yourPoliticianandMattermodels.Then your
politician_mattertable will have a PK, apolitician_idand amatter_id.Then you have
class PoliticanMatter < ActiveRecord::Base belongs_to :politician belongs_to :matter endThe 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_manywould 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
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
0 comments:
Post a Comment