Methods
- aliased_table_names_for
- build
- construct
- construct_association
- instantiate
- join_associations
- join_base
- new
Classes and Modules
Class ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociationClass ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
Attributes
| [R] | joins | |
| [R] | reflections | |
| [R] | table_aliases |
Public Class methods
[ show source ]
# File lib/active_record/associations.rb, line 1250
1250: def initialize(base, associations, joins)
1251: @joins = [JoinBase.new(base, joins)]
1252: @associations = associations
1253: @reflections = []
1254: @base_records_hash = {}
1255: @base_records_in_order = []
1256: @table_aliases = Hash.new { |aliases, table| aliases[table] = 0 }
1257: @table_aliases[base.table_name] = 1
1258: build(associations)
1259: end
Public Instance methods
[ show source ]
# File lib/active_record/associations.rb, line 1280
1280: def aliased_table_names_for(table_name)
1281: joins.select{|join| join.table_name == table_name }.collect{|join| join.aliased_table_name}
1282: end
[ show source ]
# File lib/active_record/associations.rb, line 1269
1269: def instantiate(rows)
1270: rows.each_with_index do |row, i|
1271: primary_id = join_base.record_id(row)
1272: unless @base_records_hash[primary_id]
1273: @base_records_in_order << (@base_records_hash[primary_id] = join_base.instantiate(row))
1274: end
1275: construct(@base_records_hash[primary_id], @associations, join_associations.dup, row)
1276: end
1277: return @base_records_in_order
1278: end
[ show source ]
# File lib/active_record/associations.rb, line 1261
1261: def join_associations
1262: @joins[1..-1].to_a
1263: end
[ show source ]
# File lib/active_record/associations.rb, line 1265
1265: def join_base
1266: @joins[0]
1267: end
Protected Instance methods
[ show source ]
# File lib/active_record/associations.rb, line 1285
1285: def build(associations, parent = nil)
1286: parent ||= @joins.last
1287: case associations
1288: when Symbol, String
1289: reflection = parent.reflections[associations.to_s.intern] or
1290: raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it?"
1291: @reflections << reflection
1292: @joins << JoinAssociation.new(reflection, self, parent)
1293: when Array
1294: associations.each do |association|
1295: build(association, parent)
1296: end
1297: when Hash
1298: associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
1299: build(name, parent)
1300: build(associations[name])
1301: end
1302: else
1303: raise ConfigurationError, associations.inspect
1304: end
1305: end
[ show source ]
# File lib/active_record/associations.rb, line 1307
1307: def construct(parent, associations, joins, row)
1308: case associations
1309: when Symbol, String
1310: while (join = joins.shift).reflection.name.to_s != associations.to_s
1311: raise ConfigurationError, "Not Enough Associations" if joins.empty?
1312: end
1313: construct_association(parent, join, row)
1314: when Array
1315: associations.each do |association|
1316: construct(parent, association, joins, row)
1317: end
1318: when Hash
1319: associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
1320: association = construct_association(parent, joins.shift, row)
1321: construct(association, associations[name], joins, row) if association
1322: end
1323: else
1324: raise ConfigurationError, associations.inspect
1325: end
1326: end
[ show source ]
# File lib/active_record/associations.rb, line 1328
1328: def construct_association(record, join, row)
1329: case join.reflection.macro
1330: when :has_many, :has_and_belongs_to_many
1331: collection = record.send(join.reflection.name)
1332: collection.loaded
1333:
1334: return nil if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
1335: association = join.instantiate(row)
1336: collection.target.push(association) unless collection.target.include?(association)
1337: when :has_one, :belongs_to
1338: return if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
1339: association = join.instantiate(row)
1340: record.send("set_#{join.reflection.name}_target", association)
1341: else
1342: raise ConfigurationError, "unknown macro: #{join.reflection.macro}"
1343: end
1344: return association
1345: end