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

Document that _id default value override using a Proc requires pre_processed: true option

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.4.1, 7.3.5
    • Affects Version/s: None
    • Component/s: Docs
    • Fully Compatible

      In the following example where _id field is overridden on both ends of HABTM with a default value, Mongoid writes nil instead of the _id value into the model that is created first as the foreign key value:

      task :test_3267_1 => :environment do
        class Peach
          include Mongoid::Document
      
          has_and_belongs_to_many :pits
      
          field :_id, type: String, default: -> { 'hello' }
        end
      
        class Pit
          include Mongoid::Document
      
          has_and_belongs_to_many :peaches
      
          field :_id, type: String, default: -> { 'hello' }
        end
      
        Peach.delete_all
        Pit.delete_all
      
        a = Peach.new
        b = Pit.new(peaches: [a])
        a.save!
        b.save!
      
        p Peach.last
        p Pit.last
      
        a.pits << b
      
        p ''
        p Peach.last
        p Pit.last
      end
      

      Result:

      #<Peach _id: hello, pit_ids: [nil]>
      #<Pit _id: hello, peach_ids: ["hello"]>
      ""
      #<Peach _id: hello, pit_ids: [nil, "hello"]>
      #<Pit _id: hello, peach_ids: ["hello"]>
      

      Note that the nil value stays even after the correct _id is written later via the << operator.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: