# Beginner: what to do after adding an association in a model?

**URL:** <https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641>\
**Category:** Rails\
**Created:** [March 12, 2009, 10:31am UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641 "2009-03-12T10:31:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neerolyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@neerolyte](https://www.ruby-forum.com/u/neerolyte)\
**Post date:** [March 12, 2009, 10:31am UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641/1 "2009-03-12T10:31:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![neerolyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@neerolyte](https://www.ruby-forum.com/u/neerolyte)\
**Post date:** [March 12, 2009, 10:33am UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641/2 "2009-03-12T10:33:28Z")

</div>

On Mar 12, 6:05 am, neerolyte [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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  
[Active Record Migrations — Ruby on Rails Guides](http://guides.rubyonrails.org/migrations.html) )

Fred

---

<div class="post-metadata">

**Author:** ![neerolyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@neerolyte](https://www.ruby-forum.com/u/neerolyte)\
**Post date:** [March 12, 2009, 10:34am UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641/3 "2009-03-12T10:34:03Z")

</div>

I dont think so you need to do any thing manually with the db…  
Just use the association by using dot operator.  
Regards  
Rabia

---

<div class="post-metadata">

**Author:** ![neerolyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@neerolyte](https://www.ruby-forum.com/u/neerolyte)\
**Post date:** [March 12, 2009, 11:01am UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641/4 "2009-03-12T11:01:35Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![neerolyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@neerolyte](https://www.ruby-forum.com/u/neerolyte)\
**Post date:** [March 12, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641/5 "2009-03-12T21:35:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![neerolyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@neerolyte](https://www.ruby-forum.com/u/neerolyte)\
**Post date:** [March 12, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/beginner-what-to-do-after-adding-an-association-in-a-model/162641/6 "2009-03-12T14:22:28Z")

</div>

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.
