has_manyリレーションを持つモデルのアクティブモデルJSONシリアライザをJSONレスポンスに含めることができます。RailsアクティブモデルJsonシリアライザ:has_oneとしてhas_oneというhas_manyレコードを返す
私の代わりにhas_many
レコードのhas_one
として含まれるレコードを希望 - 最初のレコードだけを含めるには:
class PersonSerializer < ActiveModel::Serializer
attributes :name, :symbol
has_many :stats
has_many :body_parts
end
、body_parts
はhas one :body_part
として返されるべきです。それをする最善の方法は何ですか?それは最初体の一部であることを正確に何を意味していますが、特定する必要があり、両方のケースで
class Person
def body_part
body_parts.first
end
# or
has_one :body_part, -> { order(id: :asc) }
end
class PersonSerializer < ActiveModel::Serializer
attributes :name, :symbol
has_many :stats
has_one :body_part
end
: