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 vendor/rails/activerecord/lib/active_record/associations.rb, line 1259
1259: def initialize(base, associations, joins)
1260: @joins = [JoinBase.new(base, joins)]
1261: @associations = associations
1262: @reflections = []
1263: @base_records_hash = {}
1264: @base_records_in_order = []
1265: @table_aliases = Hash.new { |aliases, table| aliases[table] = 0 }
1266: @table_aliases[base.table_name] = 1
1267: build(associations)
1268: end
Public Instance methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1289
1289: def aliased_table_names_for(table_name)
1290: joins.select{|join| join.table_name == table_name }.collect{|join| join.aliased_table_name}
1291: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1278
1278: def instantiate(rows)
1279: rows.each_with_index do |row, i|
1280: primary_id = join_base.record_id(row)
1281: unless @base_records_hash[primary_id]
1282: @base_records_in_order << (@base_records_hash[primary_id] = join_base.instantiate(row))
1283: end
1284: construct(@base_records_hash[primary_id], @associations, join_associations.dup, row)
1285: end
1286: return @base_records_in_order
1287: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1270
1270: def join_associations
1271: @joins[1..-1].to_a
1272: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1274
1274: def join_base
1275: @joins[0]
1276: end
Protected Instance methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1294
1294: def build(associations, parent = nil)
1295: parent ||= @joins.last
1296: case associations
1297: when Symbol, String
1298: reflection = parent.reflections[associations.to_s.intern] or
1299: raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it?"
1300: @reflections << reflection
1301: @joins << JoinAssociation.new(reflection, self, parent)
1302: when Array
1303: associations.each do |association|
1304: build(association, parent)
1305: end
1306: when Hash
1307: associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
1308: build(name, parent)
1309: build(associations[name])
1310: end
1311: else
1312: raise ConfigurationError, associations.inspect
1313: end
1314: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1316
1316: def construct(parent, associations, joins, row)
1317: case associations
1318: when Symbol, String
1319: while (join = joins.shift).reflection.name.to_s != associations.to_s
1320: raise ConfigurationError, "Not Enough Associations" if joins.empty?
1321: end
1322: construct_association(parent, join, row)
1323: when Array
1324: associations.each do |association|
1325: construct(parent, association, joins, row)
1326: end
1327: when Hash
1328: associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
1329: association = construct_association(parent, joins.shift, row)
1330: construct(association, associations[name], joins, row) if association
1331: end
1332: else
1333: raise ConfigurationError, associations.inspect
1334: end
1335: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/associations.rb, line 1337
1337: def construct_association(record, join, row)
1338: case join.reflection.macro
1339: when :has_many, :has_and_belongs_to_many
1340: collection = record.send(join.reflection.name)
1341: collection.loaded
1342:
1343: return nil if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
1344: association = join.instantiate(row)
1345: collection.target.push(association) unless collection.target.include?(association)
1346: when :has_one, :belongs_to
1347: return if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
1348: association = join.instantiate(row)
1349: record.send("set_#{join.reflection.name}_target", association)
1350: else
1351: raise ConfigurationError, "unknown macro: #{join.reflection.macro}"
1352: end
1353: return association
1354: end