Been googling trying to understand belongs_to, has_one, etc

For this scenario: 'A Person has a single favorite color and has
multiple addresses. (Colors are reused across all the persons,
addresses are always unique only for one person)'

Would the following suffice? (not worrying about adding indexes yet.)

class Person < ActiveRecord::Base
  belongs_to :favorite_color, :class_name => "Color", :foreign_key => "color_id"
  has_many :addresses, :dependent => :destroy
end

class Address < ActiveRecord::Base
  belongs_to :product
end

class Color < ActiveRecord::Base
  #don't need anything
end

The toughest thing conceptually to get a handle on was "belongs_to" on
something like Color. From an OO perspective a Person "has_one" Color,
but from reading the docs I see that belongs_to puts the foreign key
on that table in the class where you declare belongs_to so in the
above it belongs on Person. (From an OO perspective the Address
'belongs_to' a Person makes sense.)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to