method
configure_dependency_for_has_many
v1.1.6 -
Show latest stable
- Class:
ActiveRecord::Associations::ClassMethods
configure_dependency_for_has_many(reflection)private
No documentation available.
# File activerecord/lib/active_record/associations.rb, line 986
def configure_dependency_for_has_many(reflection)
if reflection.options[:dependent] && reflection.options[:exclusively_dependent]
raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options. You may specify one or the other.'
end
if reflection.options[:exclusively_dependent]
reflection.options[:dependent] = :delete_all
#warn "The :exclusively_dependent option is deprecated. Please use :dependent => :delete_all instead.")
end
# See HasManyAssociation#delete_records. Dependent associations
# delete children, otherwise foreign key is set to NULL.
# Add polymorphic type if the :as option is present
dependent_conditions = %(#{reflection.primary_key_name} = \#{record.quoted_id})
if reflection.options[:as]
dependent_conditions += " AND #{reflection.options[:as]}_type = '#{base_class.name}'"
end
case reflection.options[:dependent]
when :destroy, true
module_eval "before_destroy '#{reflection.name}.each { |o| o.destroy }'"
when :delete_all
module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }"
when :nullify
module_eval "before_destroy { |record| #{reflection.class_name}.update_all(%(#{reflection.primary_key_name} = NULL), %(#{dependent_conditions})) }"
when nil, false
# pass
else
raise ArgumentError, 'The :dependent option expects either :destroy, :delete_all, or :nullify'
end
end