Creates before_destroy callback methods that nullify, delete or destroy has_many
associated objects, according to the defined :dependent rule.
See HasManyAssociation#delete_records. Dependent associations delete
children, otherwise foreign key is set to NULL.
The extra_conditions parameter, which is not used within the main
Active Record codebase, is meant to
allow plugins to define extra finder conditions.
# File activerecord/lib/active_record/associations.rb, line 1442
def configure_dependency_for_has_many(reflection, extra_conditions = nil)
if reflection.options.include?(:dependent)
# Add polymorphic type if the :as option is present
dependent_conditions = []
dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}"
dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as]
dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
dependent_conditions << extra_conditions if extra_conditions
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
case reflection.options[:dependent]
when :destroy
method_name = "has_many_dependent_destroy_for_#{reflection.name}".to_sym
define_method(method_name) do
send(reflection.name).each { |o| o.destroy }
end
before_destroy method_name
when :delete_all
module_eval %Q{
before_destroy do |record|
delete_all_has_many_dependencies(record,
"#{reflection.name}",
#{reflection.class_name},
"#{dependent_conditions}")
end
}
when :nullify
module_eval %Q{
before_destroy do |record|
nullify_has_many_dependencies(record,
"#{reflection.name}",
#{reflection.class_name},
"#{reflection.primary_key_name}",
"#{dependent_conditions}")
end
}
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})"
end
end
end