【问题标题】:Active Model Serializer produces wrong IDActive Model Serializer 产生错误的 ID
【发布时间】:2013-04-10 11:11:08
【问题描述】:

如果您在 Rails 中使用Active Model Serializers 生成 JSON API,请参阅RailsCast 409,并像这样定义serializer..

class ArticleSerializer < ActiveModel::Serializer
  attributes :id, :name, :content
end

..并尝试序列化对象(通常通过render :json =&gt; article):

ArticleSerializer.new(article).as_json
=>  {:id=>158655280, :name => "a name", :content => "article content"}

然后它有时会产生非常奇怪的 ID 和警告:Object#id will be deprecated; use Object#object_id,至少对于 Ruby 1.8.7,Ruby 1.9.3 似乎可以工作。

这是 Ruby 问题吗?

如果无法选择更新到 Ruby 1.9.3,您该如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby serialization


    【解决方案1】:

    我有同样的问题。解决方案是在我的序列化程序中定义一个 id 方法:

    def id
      object.id
    end
    

    【讨论】:

      猜你喜欢
      • 2014-02-20
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      相关资源
      最近更新 更多