method
configure_dependency_for_has_one
v2.3.8 -
Show latest stable
- Class:
ActiveRecord::Associations::ClassMethods
configure_dependency_for_has_one(reflection)private
Creates before_destroy callback methods that nullify, delete or destroy has_one associated objects, according to the defined :dependent rule.
# File activerecord/lib/active_record/associations.rb, line 1460
def configure_dependency_for_has_one(reflection)
if reflection.options.include?(:dependent)
case reflection.options[:dependent]
when :destroy
method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.destroy unless association.nil?
end
before_destroy method_name
when :delete
method_name = "has_one_dependent_delete_for_#{reflection.name}".to_sym
define_method(method_name) do
# Retrieve the associated object and delete it. The retrieval
# is necessary because there may be multiple associated objects
# with foreign keys pointing to this object, and we only want
# to delete the correct one, not all of them.
association = send(reflection.name)
association.delete unless association.nil?
end
before_destroy method_name
when :nullify
method_name = "has_one_dependent_nullify_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.update_attribute(reflection.primary_key_name, nil) unless association.nil?
end
before_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete 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_counter_cache_callbacks -
add_limited_ids_condition! -
add_touch_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 -
delete_all_has_many_dependencies -
find_with_associations -
guard_against_unlimitable_reflections -
include_eager_conditions? -
include_eager_order? -
include_eager_select? -
join_table_name -
joined_tables -
nullify_has_many_dependencies -
order_tables -
references_eager_loaded_tables? -
reflect_on_included_associations -
select_all_rows -
select_limited_ids_list -
selects_tables -
tables_in_hash -
tables_in_string -
using_limitable_reflections?