Prev | Current Page 185 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

That is probably
unrealistic. It??™s more likely that a given farmer would own one or more tractor types and each
tractor type would be owned by one or more farmers. That is, this would have been better
implemented as a many-to-many relationship.
In theory, a many-to-many relationship is the same thing as two has_many relationships
tacked back to back. However, in a normal has_many relationship, one of the tables is supposed
to hold the foreign key of the other table. Doing that would only allow one of the tables to be
associated to many records in the other (it would only be a many-to-one relationship). In order
to keep the database normalized, a has_and_belongs_to_many (habtm) association uses an extra
table, generally referred to as a join table, which is nothing more than a pair of foreign keys for
each associated model.
Going back to our original example, let??™s say we wanted our farmer to be able to distribute
the milk from his cows. He would need to have resellers. And a reseller should be able to sell
the milk of many farmers.


Pages:
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197