Hello guys, I''m a newbie in RoR and I have a problem. My DB structure is =====================================topics table : id(int,11) name(varchar,255) categories : id (int,11) topic_id(varchar,255) name(varchar,255) =====================================--Models:-- class Topic < ActiveRecord::Base has_many :categories end class Category < ActiveRecord::Base belongs_to :topic end I need page structure like this: <h3>Topic1</h3> (id = 1) <ul><li>Category1</li> (topic_id = 1) <li>Category2</li> (topic_id = 1) <li>Category3</li> (topic_id = 1) <li>Category4</li></ul> (topic_id = 1) <h3>Topic2</h3> (id = 2) <ul><li>Category1</li> (topic_id = 2) <li>Category2</li> (topic_id = 2) <li>Category3</li> (topic_id = 2) <li>Category4</li></ul> (topic_id = 2) <h3>Topic3</h3> (id = 3) <ul><li>Category1</li> (topic_id = 3) <li>Category2</li> (topic_id = 3) <li>Category3</li> (topic_id = 3) <li>Category4</li></ul> (topic_id = 3) ... and so on. How can I do this? Thanks. -- Posted via http://www.ruby-forum.com/.
@topic = Topic.find(:all, :include => "categories") in your controller then in your view <% @topic.each do |t| %> <h3> <%= t.name %> </h3> <ul> <% t.categories.each do |c| %> <li> <%= c.name %></li> <% end %> </ul> <% end %> adam On 5/28/06, Blinchik <oladywek@gmail.com> wrote:> > Hello guys, > > I''m a newbie in RoR and I have a problem. My DB structure is > =====================================> topics table : id(int,11) > name(varchar,255) > > categories : id (int,11) > topic_id(varchar,255) > name(varchar,255) > =====================================> --Models:-- > class Topic < ActiveRecord::Base > has_many :categories > end > class Category < ActiveRecord::Base > belongs_to :topic > end > > I need page structure like this: > <h3>Topic1</h3> (id = 1) > <ul><li>Category1</li> (topic_id = 1) > <li>Category2</li> (topic_id = 1) > <li>Category3</li> (topic_id = 1) > <li>Category4</li></ul> (topic_id = 1) > <h3>Topic2</h3> (id = 2) > <ul><li>Category1</li> (topic_id = 2) > <li>Category2</li> (topic_id = 2) > <li>Category3</li> (topic_id = 2) > <li>Category4</li></ul> (topic_id = 2) > <h3>Topic3</h3> (id = 3) > <ul><li>Category1</li> (topic_id = 3) > <li>Category2</li> (topic_id = 3) > <li>Category3</li> (topic_id = 3) > <li>Category4</li></ul> (topic_id = 3) > > ... and so on. > > How can I do this? > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060528/5e6cb80c/attachment.html
Vladislav Gorodetskiy
2006-May-28 15:57 UTC
[Rails] Re: Working with topics and categories
Thanks Adam. Your answer is very helpful. I have a question again. I have 3 columns, and 9 topics with N cats. Topic 1 Topic 4 Topic 7 Cat 1 Cat 1 Cat 1 Cat 2 Cat 2 Cat 2 Cat 3 Cat 3 Cat 3 Topic 2 Topic 5 Topic 8 Cat 1 Cat 1 Cat 1 Cat 2 Cat 2 Cat 2 Cat 3 Cat 3 Cat 3 Topic 3 Topic 6 Topic 9 Cat 1 Cat 1 Cat 1 Cat 2 Cat 2 Cat 2 Cat 3 Cat 3 Cat 3 How can I get 1st, 2nd and 3rd; 4th, 5th and 6th4 7th, 8th and 9th topics from the loop? Thanks again! -- Posted via http://www.ruby-forum.com/.
well you can use tables and every 3 rows insert a new 1 column table. Or you can access the topic number order by doing t.categories[1] , t.categories[2] and so on instead of t.categories.each adam On 5/28/06, Vladislav Gorodetskiy <oladywek@gmail.com> wrote:> > Thanks Adam. Your answer is very helpful. > I have a question again. I have 3 columns, and 9 topics with N cats. > > Topic 1 Topic 4 Topic 7 > Cat 1 Cat 1 Cat 1 > Cat 2 Cat 2 Cat 2 > Cat 3 Cat 3 Cat 3 > Topic 2 Topic 5 Topic 8 > Cat 1 Cat 1 Cat 1 > Cat 2 Cat 2 Cat 2 > Cat 3 Cat 3 Cat 3 > Topic 3 Topic 6 Topic 9 > Cat 1 Cat 1 Cat 1 > Cat 2 Cat 2 Cat 2 > Cat 3 Cat 3 Cat 3 > > How can I get 1st, 2nd and 3rd; 4th, 5th and 6th4 7th, 8th and 9th > topics from the loop? > Thanks again! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060529/6fb339f8/attachment-0001.html