2011-12-06 20 views
2

Rails 3.1.3とJSON gem 1.6.3を実行しているプロジェクトがあり、ActiveRecordインスタンスをJSON.dumpに渡すことができません。私はいつも次のエラーを取得する:などJSON.dumpハッシュ、非ActiveRecordのクラスのインスタンスを、渡すActiveRecordオブジェクトのJSON.dumpが失敗します

NoMethodError: undefined method `[]=' for #<JSON::Ext::Generator::State:0x000001033f2088> 
from /Users/andy/.rvm/gems/[email protected]/gems/activerecord-3.1.3/lib/active_record/serialization.rb:10:in `serializable_hash' 
from /Users/andy/.rvm/gems/[email protected]/gems/activemodel-3.1.3/lib/active_model/serializers/json.rb:84:in `as_json' 
from /Users/andy/.rvm/gems/[email protected]/gems/activesupport-3.1.3/lib/active_support/json/encoding.rb:47:in `block in encode' 
from /Users/andy/.rvm/gems/[email protected]/gems/activesupport-3.1.3/lib/active_support/json/encoding.rb:78:in `check_for_circular_references' 
from /Users/andy/.rvm/gems/[email protected]/gems/activesupport-3.1.3/lib/active_support/json/encoding.rb:46:in `encode' 
from /Users/andy/.rvm/gems/[email protected]/gems/activesupport-3.1.3/lib/active_support/json/encoding.rb:31:in `encode' 
from /Users/andy/.rvm/gems/[email protected]/gems/activesupport-3.1.3/lib/active_support/core_ext/object/to_json.rb:20:in `to_json' 
from /Users/andy/.rvm/gems/[email protected]/gems/json-1.6.3/lib/json/common.rb:216:in `generate' 
from /Users/andy/.rvm/gems/[email protected]/gems/json-1.6.3/lib/json/common.rb:216:in `generate' 
from /Users/andy/.rvm/gems/[email protected]/gems/json-1.6.3/lib/json/common.rb:380:in `dump' 

は正常に動作し、#to_jsonは私のActiveRecordのインスタンス上で動作します。しかし、代わりに#to_jsonを呼び出すことはできません。実際にはライブラリであるため、ActiveRecordオブジェクトに渡してJSON.dumpを呼び出しています。しかし、私は同じ問題を他のライブラリをすべてスキップし、JSON.dumpを自分で呼び出すだけです。

ご協力いただきありがとうございます。

答えて

0

以下のリンクに基づいて、独自のエンコードでto_jsonをオーバーライドするJSONの問題があるようです。

スレッドで数回をポップアップした問題を回避するには、あなたのenvironments.rbファイルに次のコードを入れていました。

class Array 
    def to_json(*a) 
    ActiveSupport::JSON.encode(self) 
    end 
end 
関連する問題