# 2 levels or more nested :joins w/ AR search conditions

**URL:** <https://www.ruby-forum.com/t/2-levels-or-more-nested-joins-w-ar-search-conditions/161128>\
**Category:** Rails\
**Created:** [February 23, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/2-levels-or-more-nested-joins-w-ar-search-conditions/161128 "2009-02-23T17:43:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [February 23, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/2-levels-or-more-nested-joins-w-ar-search-conditions/161128/1 "2009-02-23T17:43:52Z")

</div>

Sorry for the ambiguous title, but here’s the problem:

Say I have tables of cities, districts and countries such that:

city belongs\_to district (district has\_many cities)  
district belongs\_to country (country has\_many districts)

Therefore, while looping through a city I can easily access the  
country’s name via city.district.country.name, and I don’t have to  
include country\_id in the city model.

I want to add a search to the city view to sort by country name. Doing  
it by district is simple using the :joins flag:

City.find(:all, :joins =\> [:district],  
:conditions =\> [“districts.name LIKE ?”, “%#{search\_text}%”])

This only goes one level deep. My question is - how can I search the  
country name through the city model? If I try the same notion as above,  
it won’t work:

City.find(:all, :joins =\> [:country],  
:conditions =\> [“countries.name LIKE ?”, “%#{search\_text}%”])

Gives the error:  
ActiveRecord::ConfigurationError: Association named ‘country’ was not  
found; perhaps you misspelled it?

Anyone had any luck with such a thing? And what if country was linked to  
another model I’d like to view through city (say, planet) - is there  
some generic solution to access several levels through the calling  
models?

I’m not going to go 20 levels down, but 2-3 sounds fair 🙂

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [February 23, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/2-levels-or-more-nested-joins-w-ar-search-conditions/161128/2 "2009-02-23T18:20:33Z")

</div>

On 23 Feb 2009, at 16:43, Shilo A. wrote:

> Gives the error:  
> ActiveRecord::ConfigurationError: Association named ‘country’ was not  
> found; perhaps you misspelled it?
> 
> Anyone had any luck with such a thing? And what if country was  
> linked to  
> another model I’d like to view through city (say, planet) - is there  
> some generic solution to access several levels through the calling  
> models?

Try the 3rd google hit for rails nested joins

Fred

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [February 24, 2009, 11:57am UTC](https://www.ruby-forum.com/t/2-levels-or-more-nested-joins-w-ar-search-conditions/161128/3 "2009-02-24T11:57:45Z")

</div>

Works great - thanks.

Here are top two links for those of you who are lazy typers:

> **[Nested includes and joins - Space Vatican](https://www.spacevatican.org/2008/8/8/nested-includes-and-joins/)**
>
> Nested eager loads are a not entirely obvious bit of rails syntax. It’s not hard once you get it though. In a nutshell you have to tell …

[http://blog.weberapps.com/?p=26](http://blog.weberapps.com/?p=26)
