你如何初始化一个ActiveRecord :: Relation数组的ActiveModel :: Serializer类?
我有一个序列化程序
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应用程序。 谢谢,
现在你可以这样做(使用AMS v0.10.2):
ActiveModel::Serializer.serializer_for(Funding.all).to_json
编辑(03.03.2017)
此方法不再有效。 使用一个Noble的答案:
ActiveModelSerializers::SerializableResource.new(Funding.all).to_json
我想这是你要找的东西:
ActiveModel::ArraySerializer.new(Funding.all, each_serializer: FundingSerializer).to_json
看到这个Thoughtbot博客文章的例子。
我不确定这是不是一个惯用的解决scheme,但它应该工作:
Funding.all.map{|f| FundingSerializer.new(f)}.to_json
您也可以明确提供集合序列化器。
render json: Funding.all, serializer: ActiveModel::ArraySerializer, each_serializer: FundingSerializer
看起来他们正在调整这个最新的ActiveModel :: Serializer gem版本。 您不再可以调用ArraySerializer上的to_json(不是ActiveModel :: Serializer :: ArraySerializer)。
这是我做了什么来解决它。
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。
这里有更多的资源潜入testing设置以供进一步阅读: http ://eclips3.net/2015/01/24/testing-active-model-serializer-with-rspec/ https://robots.thoughtbot。 COM /validationJSON的图式与-的- rspec的匹配器
testing版本>= 0.10.0
active_model_serializers
的响应我已经为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
所以你可以简单地testing:
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
我希望这可能对某人有用。
这在0.10.2
版本中0.10.2
于我:
ActiveModelSerializers::SerializableResource.new(Funding.all, each_serializer: FundingSerializer).to_json
假设你有一个序列化类(Foo)不符合你的资源名称(Bar),我使用下面的方法来轻松地序列化对象:
class BaseSerializer < ActiveModel::Serializer def self.collection_serialize(resources) ActiveModelSerializers::SerializableResource.new(resources, each_serializer: self) end end
让Foo序列化器inheritanceBaseSerializer:
class FooSerializer < BaseSerializer ... end
在控制器或外部使用FooSerializer:
bar = Bar.all FooSerializer.collection_serialize(bar).to_json