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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -