Ruby on rails 4 主动模型序列化程序和Chewy
我有一个Rails 4项目,使用AMS和Chewy 如果我为默认值定义一个AMS,我就可以很好地序列化。但是,如果我使用自定义AMS,我会得到一个错误:Ruby on rails 4 主动模型序列化程序和Chewy,ruby-on-rails-4,elasticsearch,Ruby On Rails 4,elasticsearch,我有一个Rails 4项目,使用AMS和Chewy 如果我为默认值定义一个AMS,我就可以很好地序列化。但是,如果我使用自定义AMS,我会得到一个错误:NoMethodError(未定义的方法'read\u attribute\u for#u serialization') 代码(工作正常): 代码(中断): 我编写的默认模型序列化程序和MySerializer都是相同的。那里也没有什么新奇的东西 class MySerializer < ActiveModel::Serializer
NoMethodError(未定义的方法'read\u attribute\u for#u serialization')
代码(工作正常):
代码(中断):
我编写的默认模型序列化程序和MySerializer都是相同的。那里也没有什么新奇的东西
class MySerializer < ActiveModel::Serializer
attributes :my_attribute, :other_attribute
has_one :thing
end
但是我刚刚得到了NoMethodError(未定义的方法‘my#u attribute’)结果应该是这样的
render json:@results,每个序列化程序:MySerializer
因为它试图用单个对象序列化器序列化集合
@search = Search.new(params)
@results = @search.query.load
render json: @results, serializer: MySerializer
class MySerializer < ActiveModel::Serializer
attributes :my_attribute, :other_attribute
has_one :thing
end
module Chewy
class Query
alias_method :read_attribute_for_serialization, :send
end
end