method
association_accessor_methods
association_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class)
private
Hide source
# File activerecord/lib/active_record/associations.rb, line 705 def association_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class) define_method(association_name) do |*params| force_reload = params.first unless params.empty? association = instance_variable_get("@#{association_name}") if association.nil? or force_reload association = association_proxy_class.new(self, association_name, association_class_name, association_class_primary_key_name, options) retval = association.reload unless retval.nil? instance_variable_set("@#{association_name}", association) else instance_variable_set("@#{association_name}", nil) return nil end end association end define_method("#{association_name}=") do |new_value| association = instance_variable_get("@#{association_name}") if association.nil? association = association_proxy_class.new(self, association_name, association_class_name, association_class_primary_key_name, options) end association.replace(new_value) unless new_value.nil? instance_variable_set("@#{association_name}", association) else instance_variable_set("@#{association_name}", nil) return nil end association end define_method("set_#{association_name}_target") do |target| return if target.nil? association = association_proxy_class.new(self, association_name, association_class_name, association_class_primary_key_name, options) association.target = target instance_variable_set("@#{association_name}", association) end end