method

collection_reader_method

Importance_0
v2.2.1 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::ClassMethods
collection_reader_method(reflection, 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 1290
        def collection_reader_method(reflection, association_proxy_class)
          define_method(reflection.name) do |*params|
            ivar = "@#{reflection.name}"

            force_reload = params.first unless params.empty?
            association = instance_variable_get(ivar) if instance_variable_defined?(ivar)

            unless association.respond_to?(:loaded?)
              association = association_proxy_class.new(self, reflection)
              instance_variable_set(ivar, association)
            end

            association.reload if force_reload

            association
          end

          define_method("#{reflection.name.to_s.singularize}_ids") do
            if send(reflection.name).loaded? || reflection.options[:finder_sql]
              send(reflection.name).map(&:id)
            else
              send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id)
            end
          end
        end
Register or log in to add new notes.