来自关联的活动模型序列化程序数据属性未加载
Active Model Serializers data attributes from associations are not loading
我正在尝试按如下方式加载关联:
这是我的控制器,它正在尝试渲染 json。
#app/controllers/store/products_controller.rb
def customize
@option_types = OptionType.all
respond_to do |format|
format.json { render :json => @option_types}
end
end
这是上述对象的序列化程序
#app/serializers/option_type_serializer.rb
class OptionTypeSerializer < ActiveModel::Serializer
attributes :id, :key, :customizable, :name
has_many :option_values
end
option_value belong_to
option_type
#app/serializers/option_value_serializer.rb
class OptionValueSerializer < ActiveModel::Serializer
attributes :id, :key, :option_type_id, :percentage_price_impact, :fixed_price_impact, :absolute_price_impact, :name
end
这将生成以下 JSON。尽管在 option_value_serializer.rb 中指定了许多其他参数,但在生成的 JSON
中只出现了 id 和 type
// http://0.0.0.0:3000/products/810/customize.json
{
"data": [
{
"id": "1",
"type": "option-types",
"attributes": {
"key": "fabric",
"customizable": true,
"name": "FABRIC"
},
"relationships": {
"option-values": {
"data": [
{
"id": "1",
"type": "option-values"
},
{
"id": "2",
"type": "option-values"
}
]
}
}
},
{
"id": "2",
"type": "option-types",
"attributes": {
"key": "cuff-type",
"customizable": false,
"name": "CUFF TYPE"
},
"relationships": {
"option-values": {
"data": [
]
}
}
},
{
"id": "3",
"type": "option-types",
"attributes": {
"key": "vents",
"customizable": false,
"name": "VENTS"
},
"relationships": {
"option-values": {
"data": [
]
}
}
}
]
}
忘记关联,这样做:
#app/serializers/option_type_serializer.rb
class OptionTypeSerializer < ActiveModel::Serializer
attributes :id, :key, :customizable, :name, :option_values
# has_many :option_values
def option_values
object.option_values.collect do |ov|
{
id: ov.id,
key: ov.key,
option_type_id: ov.option_type_id,
percentage_price_impact: ov.percentage_price_impact,
fixed_price_impact: ov.fixed_price_impact,
absolute_price_impact: ov.absolute_price_impact,
name: ov.name,
description: ov.description,
image: ov.option_image
}
end
end
end
下面的答案是正确的另一种方法可能是这样的:
class OptionTypeSerializer < ActiveModel::Serializer
attributes :id, :key, :customizable, :name, :option_values
# has_many :option_values
def option_values
object.option_values.collect do |ov|
OptionValueSerializer.new(ov)
end
end
end
通过使用这种方式,您不需要手动创建对象,您可以在 OptionValueSerializer
中包含装饰器,或者如果您有嵌套关系,则可以包含更多关系。
希望对您有所帮助。
我正在尝试按如下方式加载关联:
这是我的控制器,它正在尝试渲染 json。
#app/controllers/store/products_controller.rb
def customize
@option_types = OptionType.all
respond_to do |format|
format.json { render :json => @option_types}
end
end
这是上述对象的序列化程序
#app/serializers/option_type_serializer.rb
class OptionTypeSerializer < ActiveModel::Serializer
attributes :id, :key, :customizable, :name
has_many :option_values
end
option_value belong_to
option_type
#app/serializers/option_value_serializer.rb
class OptionValueSerializer < ActiveModel::Serializer
attributes :id, :key, :option_type_id, :percentage_price_impact, :fixed_price_impact, :absolute_price_impact, :name
end
这将生成以下 JSON。尽管在 option_value_serializer.rb 中指定了许多其他参数,但在生成的 JSON
中只出现了 id 和 type// http://0.0.0.0:3000/products/810/customize.json
{
"data": [
{
"id": "1",
"type": "option-types",
"attributes": {
"key": "fabric",
"customizable": true,
"name": "FABRIC"
},
"relationships": {
"option-values": {
"data": [
{
"id": "1",
"type": "option-values"
},
{
"id": "2",
"type": "option-values"
}
]
}
}
},
{
"id": "2",
"type": "option-types",
"attributes": {
"key": "cuff-type",
"customizable": false,
"name": "CUFF TYPE"
},
"relationships": {
"option-values": {
"data": [
]
}
}
},
{
"id": "3",
"type": "option-types",
"attributes": {
"key": "vents",
"customizable": false,
"name": "VENTS"
},
"relationships": {
"option-values": {
"data": [
]
}
}
}
]
}
忘记关联,这样做:
#app/serializers/option_type_serializer.rb
class OptionTypeSerializer < ActiveModel::Serializer
attributes :id, :key, :customizable, :name, :option_values
# has_many :option_values
def option_values
object.option_values.collect do |ov|
{
id: ov.id,
key: ov.key,
option_type_id: ov.option_type_id,
percentage_price_impact: ov.percentage_price_impact,
fixed_price_impact: ov.fixed_price_impact,
absolute_price_impact: ov.absolute_price_impact,
name: ov.name,
description: ov.description,
image: ov.option_image
}
end
end
end
下面的答案是正确的另一种方法可能是这样的:
class OptionTypeSerializer < ActiveModel::Serializer
attributes :id, :key, :customizable, :name, :option_values
# has_many :option_values
def option_values
object.option_values.collect do |ov|
OptionValueSerializer.new(ov)
end
end
end
通过使用这种方式,您不需要手动创建对象,您可以在 OptionValueSerializer
中包含装饰器,或者如果您有嵌套关系,则可以包含更多关系。
希望对您有所帮助。