ruby on rails - How to loop through categories posted by user in all listings -
i trying display of categories user on site has posted in. how models set up: user able create many listings. 1 many relation. listing able have multiple categories, , categories have multiple listings (there multiple listings same category). how show categories user has listed? have tried
<% @user.listings.each.unique.categories |category| %> <li><%= category %></li> <% end %>
as
<% @user.listings.categories |category| %> <li><%= category %></li> <% end %>
any recommendations on how can fix this?
you can directly this, if have has_many through inside user model
class user < applicationrecord has_many :listings has_many :categories, through: :listings end
and can
<% @user.categories |category| %> <li><%= category %></li> <% end %>
Comments
Post a Comment