This project is archived and is in readonly mode.

#3391 ✓resolved
Marcin Mańk

nested attributes vs before_save

Reported by Marcin Mańk | October 18th, 2009 @ 07:07 PM | in 2.3.6

When a nested attribute class returns false in its before_save hook, the main class still saves, and the child object silently disappears, and save returns true. Observe:

# user.rb
class User < ActiveRecord::Base
    has_many :posts
    accepts_nested_attributes_for :posts
end

#post.rb
class Post < ActiveRecord::Base
    belongs_to :user
    before_save :return_false
    def return_false
      false
    end
end

# in console
>> User.new(:name=>'bug2', :posts_attributes=>[:body=>'test2']).save!
=> true
>> User.all
=> [#<User id: 4, name: "bug2">]
>> Post.all
=> []

I think it should throw an exception (or return false in case of "save" method)

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages