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

:dependent => :nullify does not remove linkage from associated model (propose new :orphan keyword)

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 5.1.0
    • Affects Version/s: None
    • Component/s: None

      (Edited original issue)

      Currently, has_many :models, :dependent => :nullify keeps the ID intact for its associated models. For example:

      class User
        include Mongoid::Document
        has_many :comments
      
        # currently I need to do this, otherwise comment.user points to a deleted user ID
        before_destroy :orphan_comments
        protected
        def orphan_comments
          self.comments.each do |c|
            c.user = nil
            c.save
          end
        end
      
      end
      

      This is a pain when I want to write something like Comment.where(user: nil).

      I'd propose to either auto-delete the associated model ID, or maybe provide a :dependent => :orphan feature which does this.

      Using mongoid 3.0.17

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            johnnyshields Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: