# Nested has\_many :through relations fixed in 2.1.0?

**URL:** <https://www.ruby-forum.com/t/nested-has-many-through-relations-fixed-in-2-1-0/147458>\
**Category:** Rails\
**Created:** [September 2, 2008, 3:03am UTC](https://www.ruby-forum.com/t/nested-has-many-through-relations-fixed-in-2-1-0/147458 "2008-09-02T03:03:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![c\_dekker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@c\_dekker](https://www.ruby-forum.com/u/c_dekker)\
**Post date:** [September 2, 2008, 3:03am UTC](https://www.ruby-forum.com/t/nested-has-many-through-relations-fixed-in-2-1-0/147458/1 "2008-09-02T03:03:48Z")

</div>

Searching on google yields quite a lot of results describing the  
following problems:

class Person \< ActiveRecord::Base  
belongs\_to :house  
end

class House \< ActiveRecord::Base  
has\_many :people  
belongs\_to :city  
end

class City \< ActiveRecord::Base  
has\_many :houses  
has\_many :people, :through =\> :houses  
belongs\_to :country  
end

class Country \< ActiveRecord::Base  
has\_many :cities  
has\_many :houses, :through =\> :cities  
end

So far so good. Yet, I want to know all the people living in a country.  
Creating the following has\_many :through relation yields an error on  
call: has\_many :people, :through =\> :houses in the Country class. With  
the abovementioned code this is only possible by iterating through all  
coutry.houses and collecting the house.people.

Aparently in 2006 and 2007 alot of people walked into the same problems,  
yet in Rails 2.1.0 I am still unable to make said relation. Is this bug  
still not fixed, or am I doing something wrong in calling it?

---

<div class="post-metadata">

**Author:** ![c\_dekker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b782af/32.png) [@c\_dekker](https://www.ruby-forum.com/u/c_dekker)\
**Post date:** [September 2, 2008, 9:17am UTC](https://www.ruby-forum.com/t/nested-has-many-through-relations-fixed-in-2-1-0/147458/2 "2008-09-02T09:17:30Z")

</div>

On Mon, Sep 1, 2008 at 6:03 PM, Chris D. \<  
removed\_email\_address@domain.invalid\> wrote:

> belongs\_to :city  
> has\_many :houses, :through =\> :cities  
> still not fixed, or am I doing something wrong in calling it?

Hi, which statement generates the error? Do you have foreign keys on  
both  
House and City models? If not, you should have the following foreign  
keys:

City: country\_id, house\_id

House: city\_id, person\_id

From the above, your join models are House and City. Thus, I would  
recommend working with your City, House, and Person models first. For  
example, you should be able to answer the following questions:

Who lives in CityA?

city = City.find\_by\_name( ‘CityA’ )  
residents = city.people

Which City does John live?

john = Person.find\_by\_name( ‘John’ )  
city = john.city

Note: When attempting to answer these questions, both ends of the  
association must have been saved (i.e. committed to the database). For  
example, you’ll need to do the following:

person = Person.create( :name =\> ‘John’ )  
city = City.create( :name =\> ‘Chicago’ )

city.people \<\< person

After you get this working, then you can start to play with the Country,  
City, and House models. For example, you should be able to answer the  
following questions:

What houses are in CountryA?

and so on

Good luck,

-Conrad
