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

only() affects code inside blocks

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

      Hi, this doesn't seem right:

      #!/usr/bin/env ruby

      require 'mongoid'

      Mongoid.configure do |config|
      config.connect_to "mongoid_test"
      end

      class A
      include Mongoid::Document

      field :a, type: String
      field :b, type: String
      end

      class B
      include Mongoid::Document

      field :c, type: String, default: 'c has been initialized'
      end

      puts B.new.inspect

      A.delete_all
      A.create(a: 'a', b: 'b')

      A.only(:a).each do |a|
      puts B.new.inspect
      end

      Output:

      $./test_only.rb
      #<B _id: 5048f28f66d9f1e55a000001, _type: nil, c: "c has been initialized">
      #<B _id: , _type: nil, c: nil>

      I've been bitten by this again and again. I don't think anyone would expect the call to A.only() to affect B.new executed inside that block. Or any other mongoid method called on anything except maybe the yielded a object.

            Assignee:
            durran Durran Jordan
            Reporter:
            amw Adam Wróbel
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: