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

Mongoid associations' enumeration methods don't yield documents

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Critical - P2 Critical - P2
    • 9.1.0
    • Affects Version/s: 7.5.4, 8.1.5, 8.0.8
    • Component/s: Associations
    • Labels:
      None
    • Ruby Drivers

      I am using Mongodb 6 with Mongoid (Ruby ORM). I have tested this with Mongoid 7.5/8.1.5. Other dependencies:

      • Mongo: 2.19.3
      • Rails: 7.0.8.1
      • Ruby: 3.2.3

      Reproduce:

      1. Set up a new model:
      2. class Company
          # include ...
        
          has_many :connections, validate: false
        
          def default_connections
            connections.find { |c| c.default? }
          end
        end
        
        class Connection
          # include ...
        
          belongs_to :company
        
          field :default, type: Mongoid::Boolean, default: false
        end
      1. Run a test rake task
      2. namespace :issue do
          desc 'Reproduce association issue'
          task({ :association => :environment }) do |task, args|
            company = Company.new
        
            connection = company.connections.build(default: true)
        
            if company.default_connections == nil
              puts "company.default_connection is nil!!"
            end
        
            puts ''
            puts "But connection is here."
            p company.connections
        
            puts ''
            puts "#detect works fine"
            p company.connections.detect { |c| c.default? }
        
            puts ''
            puts "#method(:find).call works fine"
            p company.connections.method(:find).call { |c| c.default? }
          end
        end
        

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            erickguanst@gmail.com Erick Guan
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: