# Multiple foreign keys create / destroy

**URL:** <https://www.ruby-forum.com/t/multiple-foreign-keys-create-destroy/162316>\
**Category:** Rails\
**Created:** [March 8, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/multiple-foreign-keys-create-destroy/162316 "2009-03-08T16:11:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [March 8, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/multiple-foreign-keys-create-destroy/162316/1 "2009-03-08T16:11:39Z")

</div>

I have 4 models :

class Country \< ActiveRecord::Base  
has\_many :cities  
has\_many :streets  
has\_many :houses  
end

class City \< ActiveRecord::Base  
belongs\_to :country  
has\_many :streets  
has\_many :houses  
end

class Street \< ActiveRecord::Base  
belongs\_to :country  
belongs\_to :cities  
has\_many :houses  
end

class House \< ActiveRecord::Base  
belongs\_to :country  
belongs\_to :cities  
belongs\_to :streets  
end

Now…if i want to create a house like this:

@street = Street.find(1)  
@house = @street.houses.new  
@house.name = “Burlington house”  
@house.save

Now…only the street\_id key is set in the house record…But not  
city\_id and country\_id…  
Is there an ORM way to let active record do the job ?

Thanks

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [March 8, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/multiple-foreign-keys-create-destroy/162316/2 "2009-03-08T16:42:57Z")

</div>

On 8 Mar 2009, at 15:11, Ruby N. wrote:

> class City \< ActiveRecord::Base
> 
> class House \< ActiveRecord::Base  
> belongs\_to :country  
> belongs\_to :cities  
> belongs\_to :streets  
> end

First off, watch those belongs\_to - they should be singluar, ie  
belongs\_to :street.

> city\_id and country\_id…  
> Is there an ORM way to let active record do the job ?

No. activerecord assumes the simple case ie that the has\_many/  
belongs\_to relationship between street and house is all that it needs  
(and why do you need to duplicate data between street and house ?  
surely the country and city of the house is implicit from the street ?)

Fred

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [March 8, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/multiple-foreign-keys-create-destroy/162316/3 "2009-03-08T16:54:16Z")

</div>

Frederick C. wrote:

> On 8 Mar 2009, at 15:11, Ruby N. wrote:
> 
> > class City \< ActiveRecord::Base
> > 
> > class House \< ActiveRecord::Base  
> > belongs\_to :country  
> > belongs\_to :cities  
> > belongs\_to :streets  
> > end
> 
> First off, watch those belongs\_to - they should be singluar, ie  
> belongs\_to :street.
> 
> > city\_id and country\_id…  
> > Is there an ORM way to let active record do the job ?
> 
> No. activerecord assumes the simple case ie that the has\_many/  
> belongs\_to relationship between street and house is all that it needs  
> (and why do you need to duplicate data between street and house ?  
> surely the country and city of the house is implicit from the street ?)
> 
> Fred

Your post made it clear to me… Just a beginner…☹

Thanks

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [March 9, 2009, 6:35am UTC](https://www.ruby-forum.com/t/multiple-foreign-keys-create-destroy/162316/4 "2009-03-09T06:35:52Z")

</div>

> @house.name = “Burlington house”  
> @house.save
> 
> Now…only the street\_id key is set in the house record…But not  
> city\_id and country\_id…  
> Is there an ORM way to let active record do the job ?

That is the ORM way. The problem lies in your design (i.e.  
associations).

A country has many cities. Cities have many streets. These streets  
contain houses.  
Sure a house belongs to a city, but given the additional entities  
involved the above model makes more sense.

@house = some\_city.streets.find(1).houses.create(:name=\>‘Adelitas’)
