Forum: Ruby on Rails has_many :through

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ffdedc7137356f48e5bfcb00fce991a?d=identicon&s=25 Wiktor --- (iktorn)
on 2006-04-25 11:43
I have:

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


What is the query RoR is using when I type:

@places = @country.places

Is it only one query with joins?
2ffdedc7137356f48e5bfcb00fce991a?d=identicon&s=25 Wiktor --- (iktorn)
on 2006-04-25 13: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?
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-04-25 15: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
end

class State < ActiveRecord::Base
 has_many :cities
end

class City < ActiveRecord::Base
 has_many :places
end

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