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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thomas Balthazar wrote:> 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. > >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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oups!What I would like to do is : @province.municipalities.first.name Thanks for pointing that out, Jon. On 8/6/07, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Thomas Balthazar wrote: > > 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. > > > > > 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/ > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Balthazar <thomas.tmp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oups! > What I would like to do is : > @province.municipalities.first.name > > Thanks for pointing that out, Jon. > > > On 8/6/07, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Thomas Balthazar wrote: > > > 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. > > > > > > > > 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/ > > > > > > > > > > >-- Cheers! - Pratik http://m.onkey.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for your answer!Thomas. On 8/6/07, Pratik <pratiknaik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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 Balthazar <thomas.tmp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Oups! > > What I would like to do is : > > @province.municipalities.first.name > > > > Thanks for pointing that out, Jon. > > > > > > On 8/6/07, Jon Garvin <jgarvin.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Thomas Balthazar wrote: > > > > 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. > > > > > > > > > > > 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/ > > > > > > > > > > > > > > > > > > > -- > Cheers! > - Pratik > http://m.onkey.org > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Although it does with the has_many_through plugin [http:// code.torchbox.com/svn/rails/plugins/nested_has_many_through/] :) On Aug 6, 12:38 pm, Pratik <pratikn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Balthazar <thomas....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Oups! > > What I would like to do is : > > @province.municipalities.first.name > > > Thanks for pointing that out, Jon. > > > On 8/6/07, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Thomas Balthazar wrote: > > > > 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. > > > > 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/ > > -- > Cheers! > - Pratikhttp://m.onkey.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I wouldn''t really suggest using that plugin if you''re not on stable rails release. On 8/7/07, Aaron Pfeifer <aaron.pfeifer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Although it does with the has_many_through plugin [http:// > code.torchbox.com/svn/rails/plugins/nested_has_many_through/] :) > > On Aug 6, 12:38 pm, Pratik <pratikn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 Balthazar <thomas....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Oups! > > > What I would like to do is : > > > @province.municipalities.first.name > > > > > Thanks for pointing that out, Jon. > > > > > On 8/6/07, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Thomas Balthazar wrote: > > > > > 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. > > > > > > 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/ > > > > -- > > Cheers! > > - Pratikhttp://m.onkey.org > > > > >-- Cheers! - Pratik http://m.onkey.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---