method
configure_dependency_for_has_many
v2.1.0 -
Show latest stable
- Class:
ActiveRecord::Associations::ClassMethods
configure_dependency_for_has_many(reflection)private
See HasManyAssociation#delete_records. Dependent associations delete children, otherwise foreign key is set to NULL.
# File activerecord/lib/active_record/associations.rb, line 1259
def configure_dependency_for_has_many(reflection)
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 = 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 "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})) }"
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})"
end
end
end Related methods
- Instance methods
- belongs_to
- has_and_belongs_to_many
- has_many
- has_one
- Private methods
-
add_association_callbacks -
add_limited_ids_condition! -
add_multiple_associated_save_callbacks -
add_single_associated_save_callbacks -
association_accessor_methods -
association_constructor_method -
collection_accessor_methods -
collection_reader_method -
column_aliases -
condition_word -
conditions_tables -
configure_dependency_for_belongs_to -
configure_dependency_for_has_many -
configure_dependency_for_has_one -
construct_finder_sql_for_association_limiting -
construct_finder_sql_with_included_associations -
create_belongs_to_reflection -
create_extension_modules -
create_has_and_belongs_to_many_reflection -
create_has_many_reflection -
create_has_one_reflection -
create_has_one_through_reflection -
find_with_associations -
guard_against_unlimitable_reflections -
include_eager_conditions? -
include_eager_order? -
include_eager_select? -
join_table_name -
order_tables -
references_eager_loaded_tables? -
reflect_on_included_associations -
select_all_rows -
select_limited_ids_list -
selects_tables -
using_limitable_reflections?