# Belongs\_to more than one model

**URL:** <https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707>\
**Category:** Rails\
**Created:** [March 23, 2006, 3:57am UTC](https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707 "2006-03-23T03:57:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@Vincent\_G](https://www.ruby-forum.com/u/Vincent_G)\
**Post date:** [March 23, 2006, 3:57am UTC](https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707/1 "2006-03-23T03:57:27Z")

</div>

Suppose I have one table:

states  
id  
statename

And I have two other tables that contains states:

houses  
id  
color  
state\_id

places  
id  
place\_name  
state\_id

How would my model relationships look like?

class State \< ActiveRecord::Base  
belongs\_to house  
belongs\_to place  
end

class House \< ActiveRecord::Base  
has\_one state  
end

class Place \< ActiveRecord::Base  
has\_one state  
end

Is that right? Can I have a model that belongs to multiple models?

---

<div class="post-metadata">

**Author:** ![Vincent\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@Vincent\_G](https://www.ruby-forum.com/u/Vincent_G)\
**Post date:** [March 23, 2006, 4:07am UTC](https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707/2 "2006-03-23T04:07:08Z")

</div>

On 3/22/06, Vincent G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> color
> belongs_to house
> 
> ```
> 
> Is that right? Can I have a model that belongs to multiple models?

You can have as many belongs\_to associations as you want in one model,  
but you don’t want that with the table structure you sketched out.

The ActiveRecord setup you want is probably:  
class State \< ActiveRecord::Base  
has\_one :place  
has\_one :house  
end

class Place \< ActiveRecord::Base  
belongs\_to :state  
end

class House \< ActiveRecord::Base  
belongs\_to :state  
end

Put “belongs\_to :name” in the class that wraps a table containing a  
“name\_id” column.

–Wilson.

---

<div class="post-metadata">

**Author:** ![Vincent\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@Vincent\_G](https://www.ruby-forum.com/u/Vincent_G)\
**Post date:** [March 23, 2006, 4:46am UTC](https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707/3 "2006-03-23T04:46:49Z")

</div>

That should probably be  
class State \<ActiveRecord::Base  
has\_many :places  
has\_many :houses  
end

Unless you only want one house and one place per state…

---

<div class="post-metadata">

**Author:** ![Vincent\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@Vincent\_G](https://www.ruby-forum.com/u/Vincent_G)\
**Post date:** [March 23, 2006, 8:36am UTC](https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707/4 "2006-03-23T08:36:23Z")

</div>

Sorry, bad example. I just wanted to know if i can have multiple  
belongs\_to  
statements.

Thanks guys

---

<div class="post-metadata">

**Author:** ![Vincent\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@Vincent\_G](https://www.ruby-forum.com/u/Vincent_G)\
**Post date:** [March 23, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/belongs-to-more-than-one-model/55707/5 "2006-03-23T17:50:36Z")

</div>

Is it correct to say that the has\_one relations are not necessary unless  
you are going to be reading the state table and want to get all the  
houses that are in that state?

I.e. state.houses
