in my app users has_many categories categories have sub_categories in db i create parent_id with main category id
and now i dont know how show main category if user select only sub_category User.rb
has_many :users_ecategories
has_many :ecategories, through: :users_ecategories
Category.rb
class Ecategory < ActiveRecord::Base
has_many :users_ecategories
has_many :users, through: :users_ecategories
has_many :ecategories, class_name: 'Ecategory', foreign_key: 'parent_id'
end
Users_categories.rb class UsersCategory < ActiveRecord::Base belongs_to :user belongs_to :ecategory end
views/user/show.html.erb
<% @user.ecategories.each do |ecategory| %>
<%= ecategory.name %>
<%= ecategory.id %>
<% end %>
how to show main category name? <%= ecategory.parent_id.name %> doesn't work please help