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

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -