Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:Query for具有许多直通关联_Ruby On Rails_Activerecord_Rails Activerecord_Has Many Through - Fatal编程技术网

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)