【发布时间】:2015-11-05 13:21:11
【问题描述】:
我有以下两个序列化程序类。在索引控制器中,我想跳过加载 project_products,只显示/编辑我想获取 project_product 详细信息的方法。
class ProjectSerializer < ActiveModel::Serializer
attributes :id, :name, :category, :project_category_id, :status, :description
has_many :project_products
end
class ProjectProductSerializer < ActiveModel::Serializer
attributes :id, :name, :quantity
end
控制器:
def index
respond_with @projects
end
def load_projects
@projects = current_organization.projects.includes(:project_category)
end
【问题讨论】:
-
总结一下,你不想在 show/edit 中加载 project_products 吗?
-
嗨 @Defoncesko 正好相反,我想在显示/编辑中加载 project_products 但不在索引中
-
请显示您的视图和控制器代码。
-
def index respond_with @projects end
-
响应格式为json
标签: ruby-on-rails-4 active-model-serializers rails-api