How to move nested hash up one level in a 2-level deep association with ActiveModel::Serializer?

huangapple go评论122阅读模式
英文:

(Ruby on Rails active_model_serializers) How to move nested hash up one level in a 2-level deep association with ActiveModel::Serializer?

问题

在Ruby on Rails中,我有以下的ActiveRecord关联关系:

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

我有以下的序列化器:

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

生成的JSON如下所示:

{
  "id": 1,
  "name": "Bob",
  "car": {
    "id": 1,
    "tires": [
      {
        "id": 1
      },
      {
        "id": 2
      }
      // 其他数据
    ]
  }
}

但我希望将tires移到上一级,直接嵌套在driver下面。我想要利用现有的序列化器,而不使用Ruby的map方法。我知道可以这样做:

class DriverSerializer < ActiveModel::Serializer
  attribute :tires

  def tires
    object.tires.map{|t| TireSerializer.new(t)}
  end
end

但是否有一种更简单的方法将哈希表移到上一级?

英文:

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?

答案1

得分: 3

我认为在您的 Driver 模型中,对于 tires 属性,最好使用 has_one :tires, through: :car 关联,而不是将其委托给 :car。然后,您可以在 DriverSerializer 中为此属性使用 has_many 选项,并使用 TireSerializer 作为其序列化器。

class Driver
  has_one :car
  has_many :tires, through: :car
end

以及在您的 Driver 序列化器中:

class DriverSerializer < ActiveModel::Serializer
  has_many :tires, serializer: TireSerializer
end

参考链接

英文:

I think it's better to use has_one :tires, through: :car association for the tires attribute in you Driver model instead of delegating it to :car. You can then use the has_many option in your DriverSerializer for this attribute and use TireSerializer for its serializer.

class Driver
  has_one :car
  has_many :tires, through: :car
end

and in your Driver serializer

class DriverSerializer &lt; ActiveModel::Serializer
  has_many :tires, serializer: TireSerializer
end

huangapple
  • 本文由 发表于 2023年5月29日 19:17:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定