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

Broken autosave through an embedded model

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Autosave works great when using reference associations, but it does not appear to work through an embedded document:

      class Post
        embeds_many :comments
        belongs_to :user, autosave: true
      end
      
      class Comment
        embedded_in :post
        belongs_to :user, autosave: true
      end
      
      class User
      end
      
      comment = Comment.new(user: User.new)
      post = Post.new(user: User.new)
      post.comments << comment
      post.save
      post.user.persisted? # => true
      comment.user.persisted? # => false :(
      

      Right now, I've solved this with a before_save callback on the Post model, but it makes me sad:

      class Post
        before_save { comments.map(&:user).select(&:new_record?).each(&:save) }
      end
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            thomsbg Blake Thomson
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: