0
@@ -683,34 +683,42 @@ module ActiveRecord
0
schema_abbreviations = generate_schema_abbreviations(reflections)
0
primary_key_table = generate_primary_key_table(reflections, schema_abbreviations)
0
- rows = select_all_rows(options, schema_abbreviations, reflections)
0
- records = extract_and_instantiate_records(schema_abbreviations, rows)
0
- assign_associations_to_records(rows, records, reflections, schema_abbreviations, primary_key_table)
0
+ rows = select_all_rows(options, schema_abbreviations, reflections)
0
+ primary_key = primary_key_table[table_name]
0
+ records[id] ||= instantiate(extract_record(schema_abbreviations, table_name, row))
0
- def assign_associations_to_records(rows, records, reflections, schema_abbreviations, primary_key_table)
0
- records.each do |record|
0
reflections.each do |reflection|
0
+ next unless row[primary_key_table[reflection.table_name]]
0
when :has_many, :has_and_belongs_to_many
0
- record.send(reflection.name).target =
0
- extract_association_for_record(record, schema_abbreviations, primary_key_table, rows, reflection)
0
+ records[id].send(reflection.name)
0
+ records[id].instance_variable_get("@#{reflection.name}").target.push(
0
+ reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))
0
when :has_one, :belongs_to
0
- "set_#{reflection.name}_target",
0
- extract_association_for_record(record, schema_abbreviations, primary_key_table, rows, reflection).first
0
+ "#{reflection.name}=",
0
+ reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.table_name, row))
0
+ def reflect_on_included_associations(associations)
0
+ [ associations ].flatten.collect { |association| reflect_on_association(association) }
0
def generate_schema_abbreviations(reflections)
0
- schema = [ [ table_name, columns.collect { |c| c.name } ] ]
0
- schema += reflections.collect { |r| [ r.klass.table_name, r.klass.columns.collect { |c| c.name } ] }
0
+ schema = [ [ table_name, column_names ] ]
0
+ schema += reflections.collect { |r| [ r.table_name, r.klass.column_names ] }
0
schema_abbreviations = {}
0
schema.each_with_index do |table_and_columns, i|
0
@@ -736,9 +744,14 @@ module ActiveRecord
0
- def construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections)
0
- habtm_associations = reflections.find_all { |r| r.macro == :has_and_belongs_to_many }
0
+ def select_all_rows(options, schema_abbreviations, reflections)
0
+ connection.select_all(
0
+ construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections),
0
+ "#{name} Load Including Associations"
0
+ def construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections)
0
sql = "SELECT #{column_aliases(schema_abbreviations)} FROM #{table_name} "
0
sql << reflections.collect { |reflection| association_join(reflection) }.to_s
0
sql << "#{options[:joins]} " if options[:joins]
0
@@ -775,39 +788,14 @@ module ActiveRecord
0
- def extract_and_instantiate_records(schema_abbreviations, rows)
0
- rows.collect { |row| instantiate(extract_record(schema_abbreviations, table_name, row)) }.uniq
0
- def extract_association_for_record(record, schema_abbreviations, primary_key_table, rows, reflection)
0
- association = rows.collect do |row|
0
- if row[primary_key_table[table_name]].to_s == record.id.to_s && !row[primary_key_table[reflection.klass.table_name]].nil?
0
- reflection.klass.send(:instantiate, extract_record(schema_abbreviations, reflection.klass.table_name, row))
0
- return association.uniq.compact
0
def extract_record(schema_abbreviations, table_name, row)
0
- row.inject({}) do |record, pair|
0
- prefix, column_name = schema_abbreviations[pair.first]
0
- record[column_name] = pair.last if prefix == table_name
0
+ row.each do |column, value|
0
+ prefix, column_name = schema_abbreviations[column]
0
+ record[column_name] = value if prefix == table_name
0
- def reflect_on_included_associations(associations)
0
- [ associations ].flatten.collect { |association| reflect_on_association(association) }
0
- def select_all_rows(options, schema_abbreviations, reflections)
0
- connection.select_all(
0
- construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections),
0
- "#{name} Load Including Associations"
Comments
No one has commented yet.