| 96 | | # == Is it belongs_to or has_one? |
|---|
| | 96 | # == Cardinality and associations |
|---|
| | 97 | # |
|---|
| | 98 | # ActiveRecord associations can be used to describe relations with one-to-one, one-to-many |
|---|
| | 99 | # and many-to-many cardinality. Each model uses an association to describe its role in |
|---|
| | 100 | # the relation. In each case, the belongs_to association is used in the model that has |
|---|
| | 101 | # the foreign key |
|---|
| | 102 | # |
|---|
| | 103 | # === One-to-one |
|---|
| | 104 | # |
|---|
| | 105 | # Use has_one in the base, and belongs_to in the associated model. |
|---|
| | 106 | # |
|---|
| | 107 | # class Employee < ActiveRecord::Base |
|---|
| | 108 | # has_one :office |
|---|
| | 109 | # end |
|---|
| | 110 | # class Office < ActiveRecord::Base |
|---|
| | 111 | # belongs_to :employee # foreign key - employee_id |
|---|
| | 112 | # end |
|---|
| | 113 | # |
|---|
| | 114 | # === One-to-many |
|---|
| | 115 | # |
|---|
| | 116 | # Use has_many in the base, and belongs_to in the associated model. |
|---|
| | 117 | # |
|---|
| | 118 | # class Manager < ActiveRecord::Base |
|---|
| | 119 | # has_many :employees |
|---|
| | 120 | # end |
|---|
| | 121 | # class Employee < ActiveRecord::Base |
|---|
| | 122 | # belongs_to :manager # foreign key - employee_id |
|---|
| | 123 | # end |
|---|
| | 124 | # |
|---|
| | 125 | # === Many-to-many |
|---|
| | 126 | # |
|---|
| | 127 | # There are two ways to build a many-to-many relationship. |
|---|
| | 128 | # |
|---|
| | 129 | # The first way uses a has_many association with the :through option and a join model, so |
|---|
| | 130 | # there are two stages of associations. |
|---|
| | 131 | # |
|---|
| | 132 | # class Assignment < ActiveRecord::Base |
|---|
| | 133 | # belongs_to :programmer # foreign key - programmer_id |
|---|
| | 134 | # belongs_to :project # foreign key - project_id |
|---|
| | 135 | # end |
|---|
| | 136 | # class Programmer < ActiveRecord::Base |
|---|
| | 137 | # has_many :assignments |
|---|
| | 138 | # has_many :projects, :through => :assignments |
|---|
| | 139 | # end |
|---|
| | 140 | # class Project < ActiveRecord::Base |
|---|
| | 141 | # has_many :assignments |
|---|
| | 142 | # has_many :programmers, :through => :assignments |
|---|
| | 143 | # end |
|---|
| | 144 | # |
|---|
| | 145 | # For the second way, use has_and_belongs_to_many in both models. This requires a join table |
|---|
| | 146 | # that has no corresponding model or primary key. |
|---|
| | 147 | # |
|---|
| | 148 | # class Programmer < ActiveRecord::Base |
|---|
| | 149 | # has_and_belongs_to_many :projects # foreign keys in the join table |
|---|
| | 150 | # end |
|---|
| | 151 | # class Project < ActiveRecord::Base |
|---|
| | 152 | # has_and_belongs_to_many :programmers # foreign keys in the join table |
|---|
| | 153 | # end |
|---|
| | 154 | # |
|---|
| | 155 | # It is not always a simple decision which way of building a many-to-many relationship is best. |
|---|
| | 156 | # But if you need to work with the relationship model as its own entity, then you'll need to |
|---|
| | 157 | # use has_many :through. Use has_and_belongs_to_many when working with legacy schemas or when |
|---|
| | 158 | # you never work directly with the relationship itself. |
|---|
| | 159 | # |
|---|
| | 160 | # == Is it a belongs_to or has_one association? |
|---|