# Quick simple check: Very simple relationship setup (newbish for sure.)

**URL:** <https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885>\
**Category:** Rails\
**Created:** [July 10, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885 "2009-07-10T21:15:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/1 "2009-07-10T21:15:29Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/2 "2009-07-10T21:58:09Z")

</div>

Here’s a diagram which might help with your model:

> **[PHP Development Company - Hire Senior PHP Developers - Astec](https://astec.net/services/php/)**
>
> PHP is the perfect programming language for web application backends. Use Astec's top PHP development services to create your web application.

Make sure you also check:

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

============

The best advice I can give you is to draw a complete diagram with all  
your models either on paper or a drawing board, or in a program (your  
choice). Once you diagram the pattern you can write the relationship on  
the lines.

The reason why I say to do this goes much farther back than Rails when I  
worked with Crystal Reports. Diagrams allow you to see the associations  
better.

Looking at what you provides so far let’s go ahead and diagram it  
exactly the way you wrote it and see what’s wrong here. I’m leaving off  
activerecord base for sake of clarity…

class Person  
belongs\_to :favorite\_color, :class\_name =\> “Color”, :foreign\_key =\>  
“color\_id”  
has\_many :addresses, :dependent =\> :destroy  
end

class Address  
belongs\_to :product  
end

class Color  
#don’t need anything  
end

=============================

First thing I don’t see is your relationship models for Product or  
FavoriteColor. So, creating them based off what you wrote above I see:

class Product  
has\_many :addresses  
end

class FavoriteColor  
has\_many :persons  
has\_many :addresses, :through =\> :persons  
end

But, you have class Address belonging to Product but your association  
for Person is stating that addresses belong to it. Which does address  
really belong to?

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/3 "2009-07-10T22:41:24Z")

</div>

On Fri, Jul 10, 2009 at 3:58 PM, Älphä  
Blüë[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Address  
> FavoriteColor. So, creating them based off what you wrote above I see:
> 
> class Product  
> has\_many :addresses  
> end
> 
> class FavoriteColor  
> has\_many :persons  
> has\_many :addresses, :through =\> :persons  
> end

I thought I could do:  
belongs\_to :favorite\_color, :class\_name =\> “Color”, :foreign\_key =\>  
color\_id

Which tells it to use the Color class for favorite\_color ? I don’t  
want a FavoriteColor model, just Color. I could have just used:  
class Person  
belongs\_to :color  
If I was content with a color\_id column name for favorite color on  
Person.

> But, you have class Address belonging to Product but your association  
> for Person is stating that addresses belong to it. Which does address  
> really belong to?

Well the reason I added Addresses 'belonging to" Product is so that it  
would put the FK product\_id in the Address table. I guess I should  
just leave off belongs\_to :person on the Address model?

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/4 "2009-07-10T22:46:05Z")

</div>

And thanks for the links by the way.

But looking at the second link they show:

class Customer \< ActiveRecord::Base  
has\_many :orders, :dependent =\> :destroy  
end

class Order \< ActiveRecord::Base  
belongs\_to :customer  
end

How is that any different than my association for Person and Address  
(which you said was wrong)?

class Person \< ActiveRecord::Base  
has\_many :addresses, :dependent =\> :destroy  
end

class Address \< ActiveRecord::Base  
belongs\_to :product  
end

It looks the same as it does in the doc, unless I’m missing something?

On Fri, Jul 10, 2009 at 4:40 PM, Rick[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> > 
> > end
> 
> > But, you have class Address belonging to Product but your association  
> > for Person is stating that addresses belong to it. Which does address  
> > really belong to?
> 
> Well the reason I added Addresses 'belonging to" Product is so that it  
> would put the FK product\_id in the Address table. I guess I should  
> just leave off belongs\_to :person on the Address model?

–  
Rick R

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/5 "2009-07-10T23:30:09Z")

</div>

Using what I posted above you need to have the following:

In your Address model you need to have a

person\_id  
product\_id

If you want these foreign keys to be different you can do the following:

Class Address  
belongs\_to :person  
belongs\_to :product :foreign\_key =\> uniquesomething\_id  
end

Whatever foreign\_key you use, needs to be setup correctly in the table  
which belongs to the other table.

It really comes down to whatever you want to do and that’s why  
diagraming is good because it allows you to see what you have before you  
implement things to make sure they work well.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/6 "2009-07-10T23:20:45Z")

</div>

Rick wrote:

> And thanks for the links by the way.
> 
> But looking at the second link they show:
> 
> class Customer \< ActiveRecord::Base  
> has\_many :orders, :dependent =\> :destroy  
> end
> 
> class Order \< ActiveRecord::Base  
> belongs\_to :customer  
> end
> 
> How is that any different than my association for Person and Address  
> (which you said was wrong)?
> 
> class Person \< ActiveRecord::Base  
> has\_many :addresses, :dependent =\> :destroy  
> end
> 
> class Address \< ActiveRecord::Base  
> belongs\_to :product  
> end
> 
> It looks the same as it does in the doc, unless I’m missing something?
> 
> On Fri, Jul 10, 2009 at 4:40 PM, Rick[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > end
> > > 
> > > end
> > 
> > > But, you have class Address belonging to Product but your association  
> > > for Person is stating that addresses belong to it. ï¿½Which does address  
> > > really belong to?
> > 
> > Well the reason I added Addresses 'belonging to" Product is so that it  
> > would put the FK product\_id in the Address table. ï¿½I guess I should  
> > just leave off belongs\_to :person on the Address model?
> 
> –  
> Rick R

Hi Rick,

Thanks for looking at the links and thanks for the clarifications. I  
missed your Color class association so that part is fine.

Looking at the Customer - Order association you placed here…

Customer → Has Many → Orders  
Orders → Belongs to → Customer

That’s a correct association…

Now look at yours:

Person → Has Many → Addresses  
Address → Belongs to → (PRODUCT)?

Notice there’s no correct association here…

Anytime you you have a name\_id in any of your tables, rails  
automatically knows it’s a foreign key once you apply the association.

Person has many addresses  
Address belongs to person  
Product has many addresses  
Address belongs to product

That sound about right?

Class Person  
has\_many :addresses, :dependent =\> :destroy  
end  
Class Address  
belongs\_to :person  
belongs\_to :product  
end  
Class Product  
has\_many :addresses  
end

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 11:34pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/7 "2009-07-10T23:34:00Z")

</div>

UGGHHHH! I feel so bad.

Stupid copy and paste error! Amazing I could be staring at something  
and not even see it.

I was translating something else into Person/Color/Address from what I  
was working with “Product” and totally screwed up and put Product down  
since it was on my mind instead of Person.

So sorry for the confusion! And thanks so much for your patience and  
help. I can’t believe I was mentally translating Product into Person  
in my head:)

I meant to have:

class Address \< ActiveRecord::Base  
belongs\_to :product  
end

That makes more sense now I bet:) Apologies again.

On Fri, Jul 10, 2009 at 5:30 PM, Älphä  
Blüë[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Class Address
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Rick R

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 10, 2009, 11:34pm UTC](https://www.ruby-forum.com/t/quick-simple-check-very-simple-relationship-setup-newbish-for-sure/171885/8 "2009-07-10T23:34:36Z")

</div>

Ha i did it again sorry:

class Address \< ActiveRecord::Base  
belongs\_to :person #!!!  
end

Ok it’s Friday I need to go away !

On Fri, Jul 10, 2009 at 5:33 PM, Rick[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> help. I can’t believe I was mentally translating Product into Person
> 
> > product\_id
> 
> –  
> Rick R

–  
Rick R
