# Confused about ActiveRecord relationships

**URL:** <https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392>\
**Category:** Rails\
**Created:** [June 3, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392 "2006-06-03T23:39:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 3, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/1 "2006-06-03T23:39:49Z")

</div>

I am very confused about where to put the belongs\_to and the has\_one  
(and other relationship identifiers). I have read the RDoc and the  
agile book many times about this and I think i still see it backwards.  
Let me outline my app so you have an understanding…

I have 2 tables:

Schools { id, school\_name, address\_id }

and

Addresses { street1, street2, city, state, zip, country }

\*\*\* this is the way that makes sense to me but it doesnt work \*\*\*\*\*\*  
Models

school.rb  
has\_one :address

address.rb  
belongs\_to :school

View for School  
\<% for school in @schools %\>

\<%= school.school\_name %\>  
\<%= school.address.street1 %\>  
\<% end %\>

(I get an error referencing the school.address.street1)

\*\*\* this is the way that does not make sense but works \*\*\*\*  
Models

school.rb  
belongs\_to :address

address.rb  
has\_one :school

View for School  
\<% for school in @schools %\>

\<%= school.school\_name %\>  
\<%= school.address.street1 %\>  
\<% end %\>

I could really use some help understanding how to create the  
relationships, and how to access them properly in the views.

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 3, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/2 "2006-06-03T23:52:25Z")

</div>

Hello Nate,

> and  
> belongs\_to :school  
> […]
> 
> (I get an error referencing the school.address.street1)

Put the foreign key in addresses table and not in schools table. So :

Schools { id, school\_name }

Addresses { street1, street2, city, state, zip, country, school\_id }

Keep your models as above and it should work.

Think 'belongs\_to" as ‘refers\_to’, then your addresses table needs  
a key referencing to schools table.

HTH,

```
   -- Jean-FranÃ§ois.
```

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 12:07am UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/3 "2006-06-04T00:07:23Z")

</div>

> Put the foreign key in addresses table and not in schools table. So :
> 
> Schools { id, school\_name }
> 
> Addresses { street1, street2, city, state, zip, country, school\_id }
> 
> Keep your models as above and it should work.
> 
> Think 'belongs\_to" as ‘refers\_to’, then your addresses table needs  
> a key referencing to schools table.

Jean-FranÃ§ois,  
thank you very much for your response.  
what i didnt say earlier was that i am trying to use a single address  
table as a child table for many different tables:

schools( id, school\_name, address\_id)  
students(id, name, address\_id)  
exam(id, name, type\_id, address\_id)  
and more…

so am i going to have to have to put belongs\_to :address in each of the  
models for those tables, and has\_one :school, has\_one :student, has\_one  
:exam in the address model; even though it doesnt make sense to me?

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 12:23am UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/4 "2006-06-04T00:23:22Z")

</div>

Nate :

> has\_one :exam in the address model; even though it doesnt make  
> sense to me?

No, in fact you should polymorphic associations. The idea is to  
use an interface to which your School, Student, Exam… models  
will be associated. The Address model will be associated to  
the interface. Read carefully the wiki pages related to  
polymorphic associations :

[http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations)

And the example chosen is nearly the same as your issue !

You will have

class School \< AR::B  
has\_one :address, as :addressable  
end

and so on…

Good luck,

– Jean-FranÃ§ois.

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 12:57am UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/5 "2006-06-04T00:57:50Z")

</div>

Jean-FranÃ§ois, once again thanks for your help,

I understand how that works, and it sounds like that could work.  
The problem i see is that the example assumes that you are using 1  
address\_id per row in the associated models; but what if i had a  
customers table with a billing\_address\_id and a shipping\_address\_id?  
I’m not just making these questions up, i do have a table that has that  
situation.

Thank you,

Nate

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 1:18pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/6 "2006-06-04T13:18:39Z")

</div>

Josh’s blog entry may be helpful here.  
[http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through](http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through)  
Have a read through that and give it a go.

Hope this helps,  
-Nathan

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/7 "2006-06-04T18:08:30Z")

</div>

Jean-FranÃ§ois wrote:

> No, in fact you should polymorphic associations. The idea is to  
> use an interface to which your School, Student, Exam… models  
> will be associated. The Address model will be associated to  
> the interface. Read carefully the wiki pages related to  
> polymorphic associations :
> 
> [http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations)
> 
> And the example chosen is nearly the same as your issue !
> 
> You will have
> 
> class School \< AR::B  
> has\_one :address, as :addressable  
> end

I’m not convinced this is a good case for polymorphic associations.

Sounds like a simple case of multiple types with a belongs\_to:  
relationship with Address.

A simple foreign key is _way_ faster than a compound foreign key, which  
is essentially what polymorphic associations are.

Polymorphic associations definitely have their place. I’ve used the  
same technique in the past (in WebObjects) for things like arbitrary  
configuration objects and tagging.

Feel free to correct me. I’m new here.

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/8 "2006-06-04T23:58:22Z")

</div>

Hampton wrote:

> To honestly answer this question, we’d have to have more information  
> than  
> just the snippet. There are several ways you could model this. Three  
> that I  
> can think of off the top of my head. Each has different strengths and  
> weaknesses. So, if you can describe to us the problem domain a bit more  
> clearly, we can help much better.
> 
> -hampton.

Hampton and all,  
thanks again for this discussion and all of your advice,  
let me give you a larger snippet (hopefully inclusive enough for this  
example) so you can get a larger picture of the app.

Because I am still confused about which way to do this I will not put in  
any of the model relationships I have thus far listed; I will just give  
you the tables and allow you to see the referencing.

Tables:

Addresses  
{ id, street1, street2, city, state\_id, zip, county, country}

Professionals  
{ id, name, email, fax, phone, birthday, social\_security,  
drivers\_license\_num}

Addresses\_Professionals  
{ id, address\_id, professional\_id, address\_type\_id, start\_date,  
end\_date, public}

Address\_types  
{ id, type }

Clerkships  
{ id, professional\_id, facility\_name, start\_date, end\_date, address\_id,  
school\_id}

Licenses  
{ id, professional\_id, state\_id, address\_id, request\_address\_id,  
license\_number, license\_status\_id }

License\_status  
{ id, status }

Licensing\_exams  
{ id, professional\_id, location, address\_id, date}

I have about 15 other tables but this should encapsulate our objective  
for this discussion.

After reading about polymorphic relationships and :through and still  
being partially confused, I may just take the easy way and for each of  
the models listed above add a belongs\_to :address in it and it will  
accomplish what I need. Is this a bad way of doing it?

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 4, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/confused-about-activerecord-relationships/62392/9 "2006-06-04T18:39:16Z")

</div>

To honestly answer this question, we’d have to have more information  
than  
just the snippet. There are several ways you could model this. Three  
that I  
can think of off the top of my head. Each has different strengths and  
weaknesses. So, if you can describe to us the problem domain a bit more  
clearly, we can help much better.

-hampton.
