Belongs_to :through

belongs_to :through

Why is that not possible?

We should have:

Street
belongs_to :city
belongs_to :country, :through => :city

Wiktor wrote:

belongs_to :through

Why is that not possible?

We should have:

Street
belongs_to :city
belongs_to :country, :through => :city

Anyone?

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

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)

class Country < ActiveRecord::Base

has_many :states
has_many :cities, :through => :states
has_many :streets, :through => :cities

It doesn’t work…

Anyone?