# Belongs\_to :through

**URL:** <https://www.ruby-forum.com/t/belongs-to-through/94200>\
**Category:** Rails\
**Created:** [April 25, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/belongs-to-through/94200 "2006-04-25T12:01:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 25, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/belongs-to-through/94200/1 "2006-04-25T12:01:59Z")

</div>

belongs\_to :through

Why is that not possible?

We should have:

Street  
belongs\_to :city  
belongs\_to :country, :through =\> :city

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 26, 2006, 12:12am UTC](https://www.ruby-forum.com/t/belongs-to-through/94200/2 "2006-04-26T00:12:56Z")

</div>

Wiktor wrote:

> belongs\_to :through
> 
> Why is that not possible?
> 
> We should have:
> 
> Street  
> belongs\_to :city  
> belongs\_to :country, :through =\> :city

Anyone?

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 26, 2006, 2:37am UTC](https://www.ruby-forum.com/t/belongs-to-through/94200/3 "2006-04-26T02:37:23Z")

</div>

Wiktor wrote:

> belongs\_to :through
> 
> Why is that not possible?
> 
> We should have:
> 
> Street  
> belongs\_to :city  
> belongs\_to :country, :through =\> :city

I don’t see how that would get you much. You can simulate the convenient  
access to the country by delegating to the city, and if you are  
concerned about double queries, just use an :include on city to fetch  
the country at the same time.

Street  
belongs\_to :city, :include =\> :country  
delegate :country, :country=, :to =\> :city

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 26, 2006, 8:02am UTC](https://www.ruby-forum.com/t/belongs-to-through/94200/4 "2006-04-26T08:02:57Z")

</div>

Josh S. wrote:

> Street  
> belongs\_to :city, :include =\> :country  
> delegate :country, :country=, :to =\> :city

Josh,

It is awesome, thanks. That’s what I was looking for. Now, maybe you  
would be able to help me also with opposite problem:

class Country \< ActiveRecord::Base

has\_many :states  
has\_many :cities, :through =\> :states  
has\_many :streets, :through =\> :cities

It doesn’t work…  
I wan’t to use country.streets(…)

I was trying to use :finder\_sql =\> ‘SELECT streets.\* FROM states,  
cities, streets WHERE states.country\_id = #{id} AND cities.state\_id =  
states.id AND streets.city\_id = cities.id’ but I get an error:

Mysql::Error: #21000Operand should contain 1 column(s): SELECT \* FROM  
streets WHERE (SELECT streets.\* FROM states, cities, streets WHERE  
states.country\_id = 1 AND cities.state\_id = states.id AND  
streets.city\_id = cities.id) AND (streets.`subdomain` = ‘test’ ) LIMIT  
1

When using:  
@country.streets.find\_by\_subdomain(@sub.first)

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [June 7, 2006, 12:56am UTC](https://www.ruby-forum.com/t/belongs-to-through/94200/5 "2006-06-07T00:56:27Z")

</div>

> class Country \< ActiveRecord::Base
> 
> has\_many :states  
> has\_many :cities, :through =\> :states  
> has\_many :streets, :through =\> :cities
> 
> It doesn’t work…

Anyone?
