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

A relationship of embedded objects dumped/reloaded (to cache) becomes an array

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

      Defining a method inside an embedded relationship stops working after the object has been retrieved from cache (dumped/loaded). There're probably other instances of similar issues on embedded objects or relationships.

       ruby
      require 'spec_helper'
      
      class Display
        include Mongoid::Document
        embeds_many :tables do
          def flipped
            where(flipped: true).first
          end
        end
      end
      
      class Table
        include Mongoid::Document
        embedded_in :display
      end
      
      describe Mongoid do
      
        let(:cache) do
          ActiveSupport::Cache::MemoryStore.new
        end
      
        it "returns flipped tables" do
          display = Display.create!
          flipped_table = Table.new(flipped: true)
          display.tables << flipped_table
          display.tables.flipped.should == flipped_table
      
          2.times do
            display = cache.fetch "display" do
              display
            end
          end
      
          display.tables.flipped.should == flipped_table
        end
      
      end
      
        1) Mongoid caches and retrieves
           Failure/Error: display.tables.flipped.should == flipped_table
           NoMethodError:
             undefined method `flipped' for [#<Table _id: 511c130b3b5552e85c000002, flipped: true>]:Array
           # ./lib/mongoid/criteria.rb:510:in `method_missing'
           # ./lib/mongoid/relations/embedded/many.rb:404:in `block in method_missing'
           # ./lib/mongoid/scoping.rb:190:in `with_scope'
           # ./lib/mongoid/relations/embedded/many.rb:403:in `method_missing'
           # ./spec/dblock/new_spec.rb:35:in `block (2 levels) in <top (required)>'
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            dblock Daniel Doubrovkine
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: