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

returning false in before_create/before_update in embedded doc does not prevent save if .save is on parent

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

      class Parent
        include Mongoid::Document
        embeds_one :child, :cascade_callbacks => true
      end
      
      class Child
        include Mongoid::Document
        before_create :always_fail
        def always_fail
            puts "failing..."
            errors.add :base, "boo hoo"
            false
         end
      end
      
      1.9.3p194 :041 > p = Parent.new
       => #<Parent _id: 4ffb17ce73f05750ce000005, _type: nil> 
      1.9.3p194 :042 > p.build_child
       => #<Child _id: 4ffb17de73f05750ce000006, _type: nil> 
      1.9.3p194 :043 > p.save
      failing...
       => true 
      1.9.3p194 :044 > p.child.errors
       => #<ActiveModel::Errors:0x00000005ae7d10 @base=#<Child _id: 4ffb17de73f05750ce000006, _type: nil>, @messages={:base=>["boo hoo"]}> 
      1.9.3p194 :045 >  p.child.persisted?
       => true 
      1.9.3p194 :046 > p.child.changed?
       => false 
      

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

              Created:
              Updated:
              Resolved: