Forum: Ruby on Rails has_many :through

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Wiktor -. (Guest)
on 2006-04-25 13:43
I have:

class Country < ActiveRecord::Base
  has_many :states
  has_many :cities, :through => :states
  has_many :places, :through => :cities

What is the query RoR is using when I type:

@places = @country.places

Is it only one query with joins?
Wiktor -. (Guest)
on 2006-04-25 15:31
Wiktor  wrote:
> @places = @country.places

Replying to myself...

It doesn't seem to work - cascading :through doesn't work :(
Or am I doing something wrong?
unknown (Guest)
on 2006-04-25 17:17
(Received via mailing list)
Well for a start I think you've misunderstood the usage of has_many
:through. The idea is that its a different way of writing
has_and_belongs_to_many. Your model should be:

class Country < ActiveRecord::Base
 has_many :states

class State < ActiveRecord::Base
 has_many :cities

class City < ActiveRecord::Base
 has_many :places

Hope this helps.
This topic is locked and can not be replied to.