method

collection_accessor_methods

Importance_0
v2.3.8 - 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 1325
        def collection_accessor_methods(reflection, association_proxy_class, writer = true)
          collection_reader_method(reflection, association_proxy_class)

          if writer
            define_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

            define_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
              ids = (new_value || []).reject { |nid| nid.blank? }.map(&:to_i)
              send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
            end
          end
        end
Register or log in to add new notes.