Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1173

Enumerable#to_json doesn't work

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.0
    • Affects Version/s: None
    • Component/s: None

      class List
        include Mongoid::Document
        # Include just in case (probably already included)
        include ActiveModel::Serialization
        include ActiveModel::Serializers::JSON
      
        references_many :items
        field :name
      end
      
      class Item
        include Mongoid::Document
        field :name
      
        belongs_to :list
      end
      
      list = List.create(:name => 'list')
      1.upto(2) {|i|
        list.items.create(:name => "item_#{i}")
      }
      
      puts "list.items.to_a.to_json=" + list.items.to_a.to_json #=> list.items.to_a.to_json=[{"_id":"4e51516eb4e4cf862a000002","list_id":"4e51516eb4e4cf862a000001","name":"item_1"},{"_id":"4e51516eb4e4cf862a000003","list_id":"4e51516eb4e4cf862a000001","name":"item_2"}]
      puts "list.items.to_json=" + list.items.to_json #=> raises NoMethodError
      

      <pre>
      activemodel-3.0.10/lib/active_model/attribute_methods.rb:392:in {{method_missing': undefined method }}encode_json' for #<Item:0x00000100dedc70> (NoMethodError)
      from mongoid-2.1.9/lib/mongoid/attributes.rb:154:in `method_missing'
      </pre>

            Assignee:
            Unassigned Unassigned
            Reporter:
            romanbsd Roman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: