Ruby on rails 使用选项配置块序列化
我在rails项目中使用serialize_with_options(),并使用命名块进行渲染,如链接页面上的示例所示:Ruby on rails 使用选项配置块序列化,ruby-on-rails,ruby-on-rails-plugins,Ruby On Rails,Ruby On Rails Plugins,我在rails项目中使用serialize_with_options(),并使用命名块进行渲染,如链接页面上的示例所示: class Speaker < ActiveRecord::Base # ... serialize_with_options do methods :average_rating, :avatar_url except :email, :claim_code includes :talks end seriali
class Speaker < ActiveRecord::Base
# ...
serialize_with_options do
methods :average_rating, :avatar_url
except :email, :claim_code
includes :talks
end
serialize_with_options :with_email do
methods :average_rating, :avatar_url
except :claim_code
includes :talks
end
end
返回一个“TypeError:无法复制符号”错误。这对我来说很有意义,因为数组还没有被配置为使用serialize_with_选项。如何在运行时将此标记传递给各个speaker对象。若要使用xml并呈现所有speaker:with电子邮件?在上面的示例中,@speakers是一个数组对象。您需要在那里实现/覆盖to_xml。那么我应该工作:
class Array
def to_xml (with_email)
self.each do |element|
element.to_xml(with_email)
end
end
end
你有没有想过?我也有同样的问题。我还尝试使用@speakers.as_json(:only=>:email),但现在的问题是它试图使用定义的serialize_with_选项,因此它包括:talks和:methods.No,不幸的是我没有。我在某种程度上解决了这个问题(不记得当时是怎么解决的)。我认为要点是完全避免命名块。对不起,这没什么帮助。。。
class Array
def to_xml (with_email)
self.each do |element|
element.to_xml(with_email)
end
end
end