So building on that example,  you should add a column person_id to the
address table.  Rails will infer the foreign key based on rails model
names.  U can also specify the actual column by passing  :foreign_key
into belongs_to macro.

Roman

On Mar 12, 6:22 am, Ar Chron <[email protected]> wrote:
> The associations you create in a model have to be backed up by the
> appropriate fields in the DB.
>
> For example:
>
> class Person
>   has_many :addresses
>
> class Address
>   belongs_to :person
>
> should be a model representation of the relationship inherent in the
> database (the two really go hand-in-hand).
>
> if:
>
> Table people
>   id:integer
>   first_name:string
>   last_name:string
>
> Table address
>   id:integer
>   person_id:integer
>   line1:string
>   line2:string
>   city:string
>   state:string
>   postal_code:string
>
> has_many :addresses tells Rails that for a given person, it can use that
> person id field to retrieve address records (those whose person_id
> matches the current person id value). Similarly, from an address, Rails
> can get back to the person record by following the person_id on the
> address.
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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