2011-12-06 2 views
2

Rails 3.1.3 및 JSON gem 1.6.3을 실행하는 프로젝트가 있고 ActiveRecord 인스턴스를 JSON.dump로 전달할 수 없습니다. 나는 항상 다음과 같은 오류 얻을 :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' 

JSON.dump 등등 해시, 비 액티브 클래스의 인스턴스, 잘 작동 전달을하고, #to_json 내 액티브 인스턴스에서 작동합니다. 대신 실제로 #to_json을 호출 할 수는 없지만 실제로 라이브러리 인 것처럼 JSON.dump를 호출하는 ActiveRecord 객체를 전달하고 있습니다. 그러나 나는 다른 라이브러리를 건너 뛰고 그냥 JSON.dump를 호출하는 것과 같은 문제를 안고있다.

도움을 주시면 감사하겠습니다.

답변

0

아래 링크를 기반으로 JSON이 자체 인코딩으로 to_json을 재정의하는 것과 같은 문제가있는 것 같습니다.

몇 번이 environments.rb 파일에 다음 코드를 삽입하는 것이 었습니다 스레드에서 팝업 해결 https://rails.lighthouseapp.com/projects/8994/tickets/4494-ruby-192-heads-json-support-breaks-to_json-for-arrays-of-records

  • https://github.com/defunkt/resque/issues/149https://rails.lighthouseapp.com/projects/8994/tickets/4925-rails-3-to_json-incompatible-with-json-core-library
    • .

      class Array 
          def to_json(*a) 
          ActiveSupport::JSON.encode(self) 
          end 
      end 
      
  • 관련 문제