Active Model Serializers
Rails で JSON API を生成するために使用する場合は、 RailsCast 409serializer
を参照して、次のように定義します。
class ArticleSerializer < ActiveModel::Serializer
attributes :id, :name, :content
end
..そして、オブジェクトをシリアル化してみてください (通常は によってrender :json => 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 にアップデートできない場合、どうすれば修正できますか?