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

dependent: :nullify not working on has_one relationship

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Associations
    • None

      (Originally reported at https://github.com/mongodb/mongoid/discussions/5724)

      class HtmlContent
        include Mongoid::Document
      
        has_one :user, class_name: 'User', inverse_of: :html_content, validate: false, dependent: :nullify
      end
      
      class User
        include Mongoid::Document
      
        belongs_to :html_content, class_name: 'HtmlContent', inverse_of: :user, dependent: :destroy, validate: false, optional: true
      end
      
      user = User.create!
      html_content = HtmlContent.create!(user: user)
      
      html_content.destroy! # /lib/mongoid/association/depending.rb:110:in `_dependent_nullify!'
      # Raises an error:
      # NoMethodError: undefined method `nullify' for #<User _id: 640084c257f2fc7f67cf1862
      #
      # relation.nullify
      # ^^^^^^^^
      # /lib/mongoid/association/depending.rb:110:in `_dependent_nullify!'
      

      mongoid-7.5.3

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            jamis.buck@mongodb.com Jamis Buck
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: