Hello,
I have 4 tables Provinces, Districts, Cities and Municipalities.
A Province has many Districts. A district has many Cities, and a City
has
many Municipalities.
With this code :
class Province < ActiveRecord::Base
relationship
has_many :districts
has_many :cities, :through => :districts
end
I can do this :
@province = Province.find(:first)
@province.cities.first.name
I would like to go one step further and be able to do
@province.districts.first.name
Is it possible?
How can I do that?
Thanks for sharing your experience.
Thomas.
Oups!What I would like to do is :
@province.municipalities.first.name
Thanks for pointing that out, Jon.
Thomas B. wrote:
has_many :cities, :through => :districts
How can I do that?
Thanks for sharing your experience.
Thomas.
Unless there’s a typo in your code somewhere, I don’t think there’s any
reason that you shouldn’t be able to do that now, with what you’ve
already got.
–
http://www.5valleys.com/
AR doesn’t support nested has_many :through. You can try following :
class Province < ActiveRecord::Base
relationship
has_many :districts
has_many :cities, :through => :districts, :include =>
:municipalities
def municipalities
self.cities.map(&:municipalities)
end
end
Please note that this could nuke your memory if you’re dealing with
large datasets.
On 8/6/07, Thomas B. [email protected] wrote:
Hello,
has_many :cities, :through => :districts
How can I do that?
http://www.5valleys.com/
–
Cheers!
Although it does with the has_many_through plugin [http://
code.torchbox.com/svn/rails/plugins/nested_has_many_through/] 
Thanks for your answer!Thomas.
I wouldn’t really suggest using that plugin if you’re not on stable
rails release.
On 8/7/07, Aaron P. [email protected] wrote:
has_many :cities, :through => :districts, :include => :municipalities
Thomas B. wrote:
has_many :districts
Is it possible?
http://www.5valleys.com/
–
Cheers!
–
Cheers!