我正在尝试升级到active_model_serializers 0.10.x gem。
但是我收到了一个错误:
can't add a new key into hash during iteration
以下是代码的相关部分:
respond_to do |format|
format.html # show.html.erb
format.json { render json: @shows, each_serializer: ShowSerializer, meta: @shows.total_count, meta_key: 'count' }
end
串行:
class ShowSerializer < ActiveModel::Serializer
def attributes(*args)
data = super
performances = object.performances.order("billing_index ASC")
display_limit = 7
data[:performances] = ActiveModel::Serializer::ArraySerializer.new(performances.limit(display_limit), each_serializer: PerformanceSerializer, scope: self.scope)
data
end
end
我不使用
has_many :performances
因为我想将表演限制在第7位。
答案 0 :(得分:1)
除非您知道自己在做什么,否则不应覆盖attributes
方法。
在这里,您可以做的只是简单地覆盖关联:
class ShowSerializer < ActiveModel::Serializer
has_many :performances
def performances
object.performances.order('billing_index ASC').limit(7)
end
end