Hi, I have two models: category and subcategory. (one to many relationship), and a controller ''home'' I''d like to display all the categories and their sub categories in the view: home\index.rhtml. I have a method in the ''home'' controller'' like this: def list_categories @categories = Category.find(:all) end This list all the categories on a view, but I don''t know how to get all the sub categories that belong to their parent category. I''d like to display them like this in home\index.rhtml: Category 2 * subCategory 1 * subCategory 2 Category 2 * subCategory 3 * subCategory 4 Could anybody help me on this one? Thanks in advance! -- Best regards, Ming Ma
Ming Ma wrote:> Hi, > > I have two models: category and subcategory. (one to many > relationship), and a controller ''home'' > > I''d like to display all the categories and their sub categories in the > view: home\index.rhtml. > > I have a method in the ''home'' controller'' like this: > def list_categories > @categories = Category.find(:all) > end > > This list all the categories on a view, but I don''t know how to get > all the sub categories that belong to their parent category. I''d like > to display them like this in home\index.rhtml: > > Category 2 > * subCategory 1 > * subCategory 2 > > Category 2 > * subCategory 3 > * subCategory 4 > > Could anybody help me on this one? Thanks in advance! > > -- > Best regards, > > Ming MaMing, You can access the subcategories directly from the category. something like for subcategory in @category.subcategories ... should give you what you are looking for. Regards, Michael mmodica at cox dot net -- Posted via http://www.ruby-forum.com/.
Jeffrey Hardy
2006-Jul-23 16:53 UTC
[Rails] Newbie: Display hierarchical Records in a view
On 23-Jul-06, at 6:54 AM, Ming Ma wrote:> I''d like to display all the categories and their sub categories in the > view: home\index.rhtml.You can access a category''s children through the category itself. Pretend we''re on the console: Category.find(:first).subcategories The above will return an array of all the subcategory objects for the :first category. So, in home/index.rhtml <% @categories.each do |category| %> <%= category.name %> <% category.subcategories.each do |subcategory| %> <%= subcategory.name %> <% end %> <% end %> HTH /Jeff -- http://re.visioni.st http://quotedprintable.com
Thank you very much guys! On 7/24/06, Jeffrey Hardy <packagethief@gmail.com> wrote:> On 23-Jul-06, at 6:54 AM, Ming Ma wrote: > > I''d like to display all the categories and their sub categories in the > > view: home\index.rhtml. > > You can access a category''s children through the category itself. > Pretend we''re on the console: > > Category.find(:first).subcategories > > The above will return an array of all the subcategory objects for > the :first category. > > So, in home/index.rhtml > > <% @categories.each do |category| %> > <%= category.name %> > <% category.subcategories.each do |subcategory| %> > <%= subcategory.name %> > <% end %> > <% end %> > > HTH > > /Jeff > > > -- > http://re.visioni.st > http://quotedprintable.com > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Best regards, Ming Ma