ActiveModelSerializer 只显示关联的 id
ActiveModelSerializer only showing id for association
我正在尝试在我的 API 中使用 ActiveModelSerializer。除了 BusinessCategory 关系外,一切似乎都有效。它只是显示了它的ID。我希望它显示所有属性。我不确定它是否在使用序列化程序,因为当我删除关系时它仍然会出现。
PerksSerializer
class PerksSerializer < ActiveModel::Serializer
attributes :id, :status, :scope, :business_name, :business_description, :business_address_street,
:business_address_state, :business_address_city, :business_address_postal_code,
:business_website, :latitude, :longitude, :headline, :description, :start_date, :end_date,
:redemption_instructions, :cashier_instructions, :redemption_button_text, :claim_type,
:business_manager_approved_by, :created_at
belongs_to :primary_business_category
belongs_to :secondary_business_category
end
PerksController
def index
data = property.available_perks
render json: data
end
BusinessCategorySerializer
class BusinessCategorySerializer < ActiveModel::Serializer
attributes :name, :description
end
您可以像这样执行相同的代码:
class PerksSerializer < ActiveModel::Serializer
attributes :id, :status, :scope, :business_name, :business_description, :business_address_street,
:business_address_state, :business_address_city, :business_address_postal_code,
:business_website, :latitude, :longitude, :headline, :description, :start_date, :end_date,
:redemption_instructions, :cashier_instructions, :redemption_button_text, :claim_type,
:business_manager_approved_by, :created_at, :primary_business_category,:secondary_business_category
def primary_business_category
BusinessCategorySerializer.new(object.primary_business_category)
end
def secondary_business_category
BusinessCategorySerializer.new(object.secondary_business_category)
end
end
或
belongs_to :primary_business_category, serializer: BusinessCategorySerializer
belongs_to :secondary_business_category, serializer: BusinessCategorySerializer
检查您的 PerksSerializer 是否被调用,如果没有:
def index
data = property.available_perks
render json: data, each_serializer: PerksSerializer
end
我正在尝试在我的 API 中使用 ActiveModelSerializer。除了 BusinessCategory 关系外,一切似乎都有效。它只是显示了它的ID。我希望它显示所有属性。我不确定它是否在使用序列化程序,因为当我删除关系时它仍然会出现。
PerksSerializer
class PerksSerializer < ActiveModel::Serializer
attributes :id, :status, :scope, :business_name, :business_description, :business_address_street,
:business_address_state, :business_address_city, :business_address_postal_code,
:business_website, :latitude, :longitude, :headline, :description, :start_date, :end_date,
:redemption_instructions, :cashier_instructions, :redemption_button_text, :claim_type,
:business_manager_approved_by, :created_at
belongs_to :primary_business_category
belongs_to :secondary_business_category
end
PerksController
def index
data = property.available_perks
render json: data
end
BusinessCategorySerializer
class BusinessCategorySerializer < ActiveModel::Serializer
attributes :name, :description
end
您可以像这样执行相同的代码:
class PerksSerializer < ActiveModel::Serializer
attributes :id, :status, :scope, :business_name, :business_description, :business_address_street,
:business_address_state, :business_address_city, :business_address_postal_code,
:business_website, :latitude, :longitude, :headline, :description, :start_date, :end_date,
:redemption_instructions, :cashier_instructions, :redemption_button_text, :claim_type,
:business_manager_approved_by, :created_at, :primary_business_category,:secondary_business_category
def primary_business_category
BusinessCategorySerializer.new(object.primary_business_category)
end
def secondary_business_category
BusinessCategorySerializer.new(object.secondary_business_category)
end
end
或
belongs_to :primary_business_category, serializer: BusinessCategorySerializer
belongs_to :secondary_business_category, serializer: BusinessCategorySerializer
检查您的 PerksSerializer 是否被调用,如果没有:
def index
data = property.available_perks
render json: data, each_serializer: PerksSerializer
end