method

collection_accessor_methods

Importance_0
v3.0.9 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::ClassMethods
collection_accessor_methods(reflection, association_proxy_class, writer = true) 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 1508
        def collection_accessor_methods(reflection, association_proxy_class, writer = true)
          collection_reader_method(reflection, association_proxy_class)

          if writer
            redefine_method("#{reflection.name}=") do |new_value|
              # Loads proxy class instance (defined in collection_reader_method) if not already loaded
              association = send(reflection.name)
              association.replace(new_value)
              association
            end

            redefine_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
              pk_column = reflection.primary_key_column
              ids = (new_value || []).reject { |nid| nid.blank? }
              ids.map!{ |i| pk_column.type_cast(i) }
              send("#{reflection.name}=", reflection.klass.find(ids).index_by{ |r| r.id }.values_at(*ids))
            end
          end
        end
Register or log in to add new notes.