【发布时间】:2016-12-19 08:45:33
【问题描述】:
我需要在活动模型序列化器中的 belongs_to 和 has_one 关联的属性加上关联名称的前缀,并将其序列化为模型的直接属性。我不希望它们嵌套, 在模型下,但让它在同一水平面上平放。
例如,
class StudentSerializer
attributes :name, :email
belongs_to :school
end
class SchoolSerializer
attributes :name, :location
end
对于上面的序列化器,输出将是
{
id: 1,
name: 'John',
email: 'mail@example.com',
school: {
id: 1,
name: 'ABC School',
location: 'US'
}
}
但我需要它,
{
id: 1,
name: 'John',
email: 'mail@example.com',
school_id: 1,
school_name: 'ABC School',
school_location: 'US'
}
我可以通过将以下方法添加到序列化程序中来做到这一点
attributes :school_id, :school_name, :school_location
def school_name
object.school.name
end
def school_location
object.school.location
end
但我不认为这是一个“很好”的解决方案,因为我需要为关联中的所有属性定义方法。任何想法或解决方法(或者如果我错过了直接解决方案)来优雅地实现这一点? TIA。
更新: 我暂时使用以下解决方法对每个关联进行了处理。
attributes :school_name, :school_location
[:name, :location].each do |attr|
define_method %(school_#{attr}) do
object.school.send(attr)
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 active-model-serializers