Forum: Ruby on Rails Beginner: what to do after adding an association in a model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
370db9414d5df55cc1e6176ac44fe7fb?d=identicon&s=25 neerolyte (Guest)
on 2009-03-12 10:31
(Received via mailing list)
I've looked at a bunch of tutorials and don't really understand what
is supposed to happen after I add an association (belongs_to, has_many
etc) to both sides of a relationship.

A lot of the tutorials just add the associations and move on as if
that's it, but when I add the association nothing has changed in the
db (which makes sense because at this point I've only changed code).
Is there a command I'm supposed to run to look at all the
relationships and add tables/columns for relationships that are
missing them? or am I supposed to manually create the table?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-12 10:33
(Received via mailing list)
On Mar 12, 6:05 am, neerolyte <neerol...@gmail.com> wrote:
> I've looked at a bunch of tutorials and don't really understand what
> is supposed to happen after I add an association (belongs_to, has_many
> etc) to both sides of a relationship.
>
> A lot of the tutorials just add the associations and move on as if
> that's it, but when I add the association nothing has changed in the
> db (which makes sense because at this point I've only changed code).
> Is there a command I'm supposed to run to look at all the
> relationships and add tables/columns for relationships that are
> missing them? or am I supposed to manually create the table?

You're suppose to use migrations to add columns or create tables (see
http://guides.rubyonrails.org/migrations.html )

Fred
41c5f1c3c0fc8900b307e25777e1a2ba?d=identicon&s=25 Rabia Akhtar (Guest)
on 2009-03-12 10:34
(Received via mailing list)
I dont think so you need to do any thing manually with the db..
Just use the association by using dot operator.
Regards
Rabia
370db9414d5df55cc1e6176ac44fe7fb?d=identicon&s=25 David Schoen (Guest)
on 2009-03-12 11:01
(Received via mailing list)
Does that mean I have to write a custom migration for it, the only
automated migrations I can find are for adding/removing columns
(AddXXXtoYYY).

Is there something like AddHasAndBelongsToManyModelXToModelY?

Dave.

2009/3/12 Frederick Cheung <frederick.cheung@gmail.com>:
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-03-12 14:22
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.
9f09476d4ff354dc7c8d7676e5974e1e?d=identicon&s=25 Roman Rozinov (rroman81)
on 2009-03-12 21:35
(Received via mailing list)
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
This topic is locked and can not be replied to.