mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
5aab0c0538
Fixes #12812 Raise `ActiveRecord::RecordNotDestroyed` when a child marked with `dependent: destroy` can't be destroyed. The following code: ```ruby class Post < ActiveRecord::Base has_many :comments, dependent: :destroy end class Comment < ActiveRecord::Base before_destroy do return false end end post = Post.create!(comments: [Comment.create!]) post.comments = [Comment.create!] ```` would result in a `post` with two `comments`. With this commit, the same code would raise a `RecordNotDestroyed` exception, keeping the `post` with the same `comment`.
51 lines
1.1 KiB
Ruby
51 lines
1.1 KiB
Ruby
class Bulb < ActiveRecord::Base
|
|
default_scope { where(:name => 'defaulty') }
|
|
belongs_to :car, :touch => true
|
|
|
|
attr_reader :scope_after_initialize, :attributes_after_initialize
|
|
|
|
after_initialize :record_scope_after_initialize
|
|
def record_scope_after_initialize
|
|
@scope_after_initialize = self.class.all
|
|
end
|
|
|
|
after_initialize :record_attributes_after_initialize
|
|
def record_attributes_after_initialize
|
|
@attributes_after_initialize = attributes.dup
|
|
end
|
|
|
|
def color=(color)
|
|
self[:color] = color.upcase + "!"
|
|
end
|
|
|
|
def self.new(attributes = {}, &block)
|
|
bulb_type = (attributes || {}).delete(:bulb_type)
|
|
|
|
if bulb_type.present?
|
|
bulb_class = "#{bulb_type.to_s.camelize}Bulb".constantize
|
|
bulb_class.new(attributes, &block)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
end
|
|
|
|
class CustomBulb < Bulb
|
|
after_initialize :set_awesomeness
|
|
|
|
def set_awesomeness
|
|
self.frickinawesome = true if name == 'Dude'
|
|
end
|
|
end
|
|
|
|
class FunkyBulb < Bulb
|
|
before_destroy do
|
|
raise "before_destroy was called"
|
|
end
|
|
end
|
|
|
|
class FailedBulb < Bulb
|
|
before_destroy do
|
|
false
|
|
end
|
|
end
|