method

find_with_associations

Importance_0
v1.0.0 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::ClassMethods
find_with_associations(options = {}) 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 847
        def find_with_associations(options = {})
          reflections  = reflect_on_included_associations(options[:include])

          guard_against_missing_reflections(reflections, options)
          
          schema_abbreviations = generate_schema_abbreviations(reflections)
          primary_key_table    = generate_primary_key_table(reflections, schema_abbreviations)

          rows                      = select_all_rows(options, schema_abbreviations, reflections)
          records, records_in_order = { }, []
          primary_key               = primary_key_table[table_name]
          
          for row in rows
            id = row[primary_key]
            records_in_order << (records[id] = instantiate(extract_record(schema_abbreviations, table_name, row))) unless records[id]
            record = records[id]

            reflections.each do |reflection|
              case reflection.macro
                when :has_many, :has_and_belongs_to_many
                  collection = record.send(reflection.name)
                  collection.loaded

                  next unless row[primary_key_table[reflection.table_name]]

                  association = reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))                  
                  collection.target.push(association) unless collection.target.include?(association)
                when :has_one, :belongs_to
                  next unless row[primary_key_table[reflection.table_name]]

                  record.send(
                    "set_#{reflection.name}_target", 
                    reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))
                  )
              end
            end
          end
          
          return records_in_order
        end
Register or log in to add new notes.