I have the following ActiveRecord associations in Ruby on Rails:
class Driver
has_one :car
delegate :tires, to: :car
end
class Car
belongs_to :driver
has_many :tires
end
class Tire
belongs_to :car
end
I have the following Serializer:
class DriverSerializer < ActiveModel::Serializer
attributes :id, :name
has_one :car
class CarSerializer < ActiveModel::Serializer
attributes :id
has_many :tires
class TireSerializer < ActiveModel::Serializer
attributes :id
end
end
end
The resulting JSON looks like this:
{
"id": 1,
"name": "Bob",
"car": {
"id": 1,
"tires": [
{
"id": 1
},
{
"id": 2
}
...
]
}
}
But I want the tires to move up one level so that they're nested directly under driver. I want to leverage the serializers as-is, without using Ruby's map method. I know I can do this:
class DriverSerializer < ActiveModel::Serializer
attribute :tires
def tires
object.tires.map{|t| TireSerializer.new(t)}
end
end
But is there a simpler way to just move the hash up one level?
I think it's better to use
has_one :tires, through: :carassociation for thetiresattribute in youDrivermodel instead of delegating it to:car. You can then use thehas_manyoption in yourDriverSerializerfor this attribute and useTireSerializerfor its serializer.and in your Driver serializer