Recursive categories fetch and view
Here's how I'd do it:

In your view, simply call a recursive method on each category:

&lt;ul&gt;
  &lt;% Category.roots.map do |category| %&gt;
     &lt;%= draw_tree(category) %&gt;
  &lt;% end %&gt;
&lt;/ul&gt;


And now in a helper file (app/helpers/category_helper.rb?), this is the "right" place to put HTML helpers. First, in pseudo-code:

def draw_tree(category)
  &lt;li class="col-md-2"&gt;
    &lt;a href="#"&gt;&lt;%= category.name %&gt;&lt;/a&gt;
    &lt;% category.children.each do |child| %&gt;
    &lt;ul&gt;
      &lt;%= draw_tree(child) %&gt;
    &lt;/ul&gt;
    &lt;% end %&gt;
  &lt;/li&gt;
end


Now, to actually make this work, rather than writing a string and calling #html_safe, a much cleaner (and generally safer) approach is to use content_tag. The end result is:

def draw_tree(category)
  content_tag(:li, class: "col-md-2") do
    link_to('#', category.name)
    category.children.map do |child|
      content_tag(:ul, draw_tree(child))
    end
  end
end


I haven't actually run the above code, so apologies if there's some small mistake. But hopefully you get the idea :)