64842

Ruby On Rails Relationships - One to Many

Question:

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?

Answer1:

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.

Then your politician_matter table will have a PK, a politician_id and a 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.

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

Answer2:

You need a many2many relationship between these two entities.

<ul><li>A matter can be studied by many people</li> <li>A person can studie several matters</li> </ul>

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!

<a href="http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001888&name=has_and_belongs_to_many" rel="nofollow">has_and_belongs_to_many helper</a>

Answer3:

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)

Hope this helps Seb

Recommend

  • Ruby On Rails Relationships - One to Many
  • Are There Any Other Web Programming Languages That Can Be Used Without A Framework Aside From PHP?
  • Ruby on Rails and WebSphere Can I have both and either?
  • oocss-compass plugin setup
  • Ajax request sent via GET even if method is POST
  • Javascript + MVC + UI
  • Trouble on using the i18n gem with partial template files
  • Rails Migration: How to increase INTEGER size by using ROR migration
  • What is the difference between Ruby and ROR? [duplicate]
  • what is the equivalent of “data-hover:” in a ruby on rails' link_to block
  • Rotate left/right with number of moves using GCC assembly
  • Unity 5 WebGL: Communicating with external javascript from C#
  • Need help at dynamic table content plan in rails 4
  • How do I calculate the difference in days between two ISO8601 dates with Freemarker?
  • when should i use multiple controllers in mvc?
  • Ruby / Rails - Hide Navbar & Footer on Root Page [duplicate]
  • Difference between and in RoR [duplicate]
  • interpolation in 3d computer graphics
  • How to properly add strings to QListWidgets?
  • Converting Twitter DateTime to Localtime with adding UTC offset [duplicate]
  • Random access image (picture) file
  • What is the best Linux distro to work with Ruby on Rails? [closed]
  • joining two bezier curves
  • Read a local file using javascript
  • Django: Count of Group Elements
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • How to make Safari send if-modified-since header?
  • How to get next/previous record number?
  • How to pass list parameters for each object using Spring MVC?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • JaxB to read class hierarchy
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?