45

我有一个序列化器

class FundingSerializer < ActiveModel::Serializer
  attributes :id, 

  has_one :user
  has_one :tournament
  embed :ids, include: true
end

使用适当的关联进行初始化

FundingSerializer.new(Funding.first).to_json

产量

"{\"users\":[{\"id\":2,\"first_name\":\"Nick\"}],\"tournaments\":[{\"id\":1,\"end_date\":\"2013-07-21T23:18:54.981Z\",\"start_date\":\"2013-07-14T23:18:54.980Z\"}],\"funding\":{\"id\":1}}"

但,

FundingSerializer.new(Funding.all).to_json

得到这个错误。

undefined method `read_attribute_for_serialization' for #<ActiveRecord::Relation::ActiveRecord_Relation_Funding:0x007f998910a250>
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:121:in `method_missing'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:68:in `method_missing'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:99:in `block in attribute'
    from (eval):3:in `_fast_attributes'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:466:in `rescue in attributes'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:454:in `attributes'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:478:in `_serializable_hash'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:360:in `serializable_hash'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:344:in `as_json'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:50:in `block in encode'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:49:in `encode'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:34:in `encode'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/activesupport-4.0.0/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:333:in `to_json'
    from (irb):1
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/nicholasshook/.rvm/gems/ruby-2.0.0-p247@pokerfund/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'

我不想简单地渲染 json: Funding.all,因为我想将此 json 传递给我的 rails 应用程序和 angularjs 应用程序中的其他对象。谢谢,

4

8 回答 8

174

我想这就是你要找的:

ActiveModel::ArraySerializer.new(Funding.all, each_serializer: FundingSerializer).to_json

有关示例,请参阅此Thoughtbot 博客文章

于 2013-07-29T01:46:39.797 回答
28

我不确定这是否是一个惯用的解决方案,但它应该可以工作:

Funding.all.map{|f| FundingSerializer.new(f)}.to_json
于 2013-07-10T14:14:58.557 回答
9

这在版本中对我有用0.10.2

ActiveModelSerializers::SerializableResource.new(Funding.all, each_serializer: FundingSerializer).to_json

于 2016-09-21T02:21:00.337 回答
8

现在您可以通过这种方式执行此操作(使用 AMS v0.10.2):

ActiveModel::Serializer.serializer_for(Funding.all).to_json

编辑(03.03.2017)
此方法不再有效。使用aNoble 的答案:

ActiveModelSerializers::SerializableResource.new(Funding.all).to_json
于 2016-09-04T15:39:00.427 回答
6

看起来他们在最新的 ActiveModel::Serializers gem 版本中再次对此进行了调整。您不再可以在 ArraySerializer(不是 ActiveModel::Serializer::ArraySerializer)上调用 to_json。

这是我为解决它所做的。

let(:resource)        { Funding.all }
let(:serializer)      { ActiveModel::Serializer::ArraySerializer.new(resource, each_serializer: FundingSerializer)
let(:serialization)   { ActiveModel::Serializer::Adapter.create(serializer) }
subject               { JSON.parse(serialization.to_json) }

调用主题将为您提供所需的 json。

这里还有一些资源可以深入测试设置以供进一步阅读: http ://eclips3.net/2015/01/24/testing-active-model-serializer-with-rspec/ https://robots.thoughtbot。 com/validating-json-schemas-with-an-rspec-matcher

于 2015-06-17T16:44:23.130 回答
6

您也可以显式提供集合序列化程序。

render json: Funding.all, serializer: ActiveModel::ArraySerializer, each_serializer: FundingSerializer
于 2015-09-24T18:53:09.873 回答
3

active_model_serializers使用版本测试响应>= 0.10.0我已经为 RSpec 完成了这个简单的助手:

module AMSHelper
  def ams_array_serializer(collection, options: {}, adapter: :json)
    serializer = ActiveModel::Serializer::ArraySerializer.new(collection)
    adapter_class = ActiveModel::Serializer::Adapter.adapter_class(adapter)
    adapter_class.new(serializer, options)
  end

  def ams_serializer(object, options: {}, adapter: :json)
    serializer = ActiveModel::Serializer.serializer_for(object).new(object)
    adapter_class = ActiveModel::Serializer::Adapter.adapter_class(adapter)

    adapter_class.new(serializer, options)
  end
end

RSpec.configure do |config|
  config.include AMSHelper, type: :request
end

所以你可以简单地测试:

RSpec.describe "Posts", type: :request do
  describe "GET /" do
    it "returns http success" do
      get posts_path
      expect(response_body).to eq(ams_array_serializer(Post.all).to_json)
    end
  end
end

我希望这对某人有用。

于 2015-07-13T13:20:09.673 回答
1

假设您有一个序列化程序类(Foo)与您的资源名称(Bar)不匹配,我使用以下方法轻松序列化对象:

class BaseSerializer < ActiveModel::Serializer
  def self.collection_serialize(resources)
    ActiveModelSerializers::SerializableResource.new(resources, each_serializer: self)
  end
end

让 Foo 序列化器继承 BaseSerializer:

class FooSerializer < BaseSerializer
  ...
end

在控制器或外部使用 FooSerializer:

bar = Bar.all
FooSerializer.collection_serialize(bar).to_json
于 2016-10-12T05:11:20.063 回答