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

delete_all method ignores :dependent option on referenced 1-N relationship

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.7
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Using mongoid 2.4.6 on rails 3.2.2. The documentation for referenced 1-n relationships states that if a relationship has :dependent => :nullify (the default option), then all calls to delete on the relationship will nullify the relationship not destroy. Calling delete_all on the has_many relationship seems to ignore this option and call delete on all of the related documents regardless. Consider this setup:

      document Calendar
      include Mongoid::Document

      has_many :days # :dependent => :nullify is the default here so it's not necessary to declare
      end

      document Day
      include Mongoid::Document

      belongs_to :calendar
      end

      Now say you have a calendar document with 3 related day documents. Calling clear on the relationship like so:

      calendar.days.clear

      or setting the days relationship to an empty array like so:

      calendar.days = []

      will nullify the related days as expected. However, if you call delete_all, like so:

      calendar.days.delete_all

      instead of nullifying the related days as expected, it appears to call delete on each individual day, and they all then get removed from the database.

            Assignee:
            Unassigned Unassigned
            Reporter:
            batter batter
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              None
              None
              None
              None