activerecord - Rails: Query for has many through associations -


i have following associations

class post < activerecord::base   has_many :categorizations   has_many :categories, through: :categorizations  class categorization < activerecord::base   belongs_to :category   belongs_to :post end  class category < activerecord::base   has_many :categorizations   has_many :posts, through: :categorizations end 

for user posts,

@posts = current_user.posts 

i need categories associated @posts. need this

@categories = @posts.categories 

so how can associated categories of posts.

you can use includes load categories of each post

current_user.posts.includes(:categories) 

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? -