Answer the question
In order to leave comments, you need to log in
Ruby on rails: How to display a hierarchical list?
You need to create a nested category hierarchy in your project.
Migration:
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :title
t.references :parent, index: true
t.timestamps null: false
end
end
end
class Category < ActiveRecord::Base
has_many :subordinates, class_name: "Category",
foreign_key: "parent_id"
belongs_to :parent, class_name: "Category"
end
Answer the question
In order to leave comments, you need to log in
The following code worked correctly:
<% if category.subordinates.count >=1 %>
<%= category.subordinates.first.title %>
<% end %>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question