-
Type: Bug
-
Resolution: Unresolved
-
Priority: Critical - P2
-
Affects Version/s: 7.5.4, 8.1.5, 8.0.8
-
Component/s: Associations
-
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:
- Set up a new model:
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
- Run a test rake task
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