method

collection_accessor_methods

Importance_0
v1.0.0 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::ClassMethods
collection_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class) private

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/associations.rb, line 751
        def collection_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}")
            unless association.respond_to?(:loaded?)
              association = association_proxy_class.new(self,
                association_name, association_class_name,
                association_class_primary_key_name, options)
              instance_variable_set("@#{association_name}", association)
            end
            association.reload if force_reload
            association
          end

          define_method("#{association_name}=") do |new_value|
            association = instance_variable_get("@#{association_name}")
            unless association.respond_to?(:loaded?)
              association = association_proxy_class.new(self,
                association_name, association_class_name,
                association_class_primary_key_name, options)
              instance_variable_set("@#{association_name}", association)
            end
            association.replace(new_value)
            association
          end

          define_method("#{Inflector.singularize(association_name)}_ids=") do |new_value|
            send("#{association_name}=", association_class_name.constantize.find(new_value))
          end
        end
Register or log in to add new notes.