Ruby on rails Rails:Query for具有许多直通关联
我有以下联想Ruby on rails Rails:Query for具有许多直通关联,ruby-on-rails,activerecord,rails-activerecord,has-many-through,Ruby On Rails,Activerecord,Rails Activerecord,Has Many Through,我有以下联想 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::Bas
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
我需要与@posts关联的所有类别。所以我需要这样的东西
@categories = @posts.categories
因此,如何获取相关的帖子类别。您可以使用includes加载每个帖子的类别
current_user.posts.includes(:categories)
current_user.posts.includes(:categories)