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

Autosave behavior and specs are inconsistent with inline documentation

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

      We ran into an issue where validates_presence_of callbacks were triggering autosave on relations that already existed and subsequently triggered unnecessary validations and additional autosaves. We looked at both the Mongoid source and documentation and they seem to be inconsistent:

      https://github.com/mongoid/mongoid/blob/master/lib/mongoid/relations/auto_save.rb#L49

      # Set up the autosave behaviour for references many and references one
      # relations. When the option is set to true, these relations will get
      # saved automatically when the parent is first saved, but not if the
      # parent already exists in the database.
      

      It turns out that Mongoid::Relations::AutoSave::ClassMethods#autosave does not check whether the parent document already exists and always triggers autosave on our relations. The specs are also expecting this behavior:

      https://github.com/mongoid/mongoid/blob/master/spec/mongoid/relations/auto_save_spec.rb#L85
      https://github.com/mongoid/mongoid/blob/master/spec/mongoid/relations/auto_save_spec.rb#L117
      https://github.com/mongoid/mongoid/blob/master/spec/mongoid/relations/auto_save_spec.rb#L153

      Our workaround is to explicitly state autosave: false on our belongs_to relationships so that autosave does not get triggered, but would appreciate if either the documented behavior was implemented or fixed. Unfortunately the way I see it implementing the documented behavior would be breaking a lot of codebase so we will have to stick with explicitly disabling autosave, but would love to hear your thoughts, @durran.

      Thanks.

      /cc lackac

            Assignee:
            Unassigned Unassigned
            Reporter:
            rgabo Gabor Ratky
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: