我有一個串行如何使用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
得到此電子郵件RROR。
undefined method `read_attribute_for_serialization' for #<ActiveRecord::Relation::ActiveRecord_Relation_Funding:0x007f998910a250>
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:121:in `method_missing'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:68:in `method_missing'
from /Users/nicholasshook/.rvm/gems/[email protected]/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/[email protected]/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:466:in `rescue in attributes'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:454:in `attributes'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:478:in `_serializable_hash'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:360:in `serializable_hash'
from /Users/nicholasshook/.rvm/gems/[email protected]/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[email protected]/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:50:in `block in encode'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:49:in `encode'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/activesupport-4.0.0/lib/active_support/json/encoding.rb:34:in `encode'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/activesupport-4.0.0/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:333:in `to_json'
from (irb):1
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/nicholasshook/.rvm/gems/[email protected]/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
我不想簡單地渲染JSON:Funding.all因爲我想各地通過這個JSON的其他對象在我的Rails應用程序,並與angularjs應用。謝謝,
雖然,[SandOnTeeth的答案](http://stackoverflow.com/a/39318636/1836143)可能會奏效,這不是官方的API 。請參閱[文檔](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/outside_controller_use.md#using-activemodelserializers-outside-of-a-controller)。 –