Class: ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
- Inherits:
-
JoinBase
- Object
- JoinBase
- ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
- Defined in:
- lib/active_record/associations.rb
Instance Attribute Summary collapse
-
#aliased_join_table_name ⇒ Object
readonly
Returns the value of attribute aliased_join_table_name.
-
#aliased_prefix ⇒ Object
readonly
Returns the value of attribute aliased_prefix.
-
#aliased_table_name ⇒ Object
readonly
Returns the value of attribute aliased_table_name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_table_name ⇒ Object
readonly
Returns the value of attribute parent_table_name.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Attributes inherited from JoinBase
Instance Method Summary collapse
- #association_join ⇒ Object
-
#initialize(reflection, join_dependency, parent = nil) ⇒ JoinAssociation
constructor
A new instance of JoinAssociation.
Methods inherited from JoinBase
#aliased_primary_key, #column_names_with_alias, #extract_record, #instantiate, #record_id
Constructor Details
#initialize(reflection, join_dependency, parent = nil) ⇒ JoinAssociation
Returns a new instance of JoinAssociation.
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 |
# File 'lib/active_record/associations.rb', line 1372 def initialize(reflection, join_dependency, parent = nil) reflection.check_validity! if reflection.[:polymorphic] raise EagerLoadPolymorphicError.new(reflection) end super(reflection.klass) @parent = parent @reflection = reflection @aliased_prefix = "t#{ join_dependency.joins.size }" @aliased_table_name = table_name # start with the table name @parent_table_name = parent.active_record.table_name if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{aliased_table_name.downcase}\son} join_dependency.table_aliases[aliased_table_name] += 1 end unless join_dependency.table_aliases[aliased_table_name].zero? # if the table name has been used, then use an alias @aliased_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}" table_index = join_dependency.table_aliases[aliased_table_name] @aliased_table_name = @aliased_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0 end join_dependency.table_aliases[aliased_table_name] += 1 if reflection.macro == :has_and_belongs_to_many || (reflection.macro == :has_many && reflection.[:through]) @aliased_join_table_name = reflection.macro == :has_and_belongs_to_many ? reflection.[:join_table] : reflection.through_reflection.klass.table_name unless join_dependency.table_aliases[aliased_join_table_name].zero? @aliased_join_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}_join" table_index = join_dependency.table_aliases[aliased_join_table_name] @aliased_join_table_name = @aliased_join_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0 end join_dependency.table_aliases[aliased_join_table_name] += 1 end end |
Instance Attribute Details
#aliased_join_table_name ⇒ Object (readonly)
Returns the value of attribute aliased_join_table_name.
1369 1370 1371 |
# File 'lib/active_record/associations.rb', line 1369 def aliased_join_table_name @aliased_join_table_name end |
#aliased_prefix ⇒ Object (readonly)
Returns the value of attribute aliased_prefix.
1369 1370 1371 |
# File 'lib/active_record/associations.rb', line 1369 def aliased_prefix @aliased_prefix end |
#aliased_table_name ⇒ Object (readonly)
Returns the value of attribute aliased_table_name.
1369 1370 1371 |
# File 'lib/active_record/associations.rb', line 1369 def aliased_table_name @aliased_table_name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
1369 1370 1371 |
# File 'lib/active_record/associations.rb', line 1369 def parent @parent end |
#parent_table_name ⇒ Object (readonly)
Returns the value of attribute parent_table_name.
1369 1370 1371 |
# File 'lib/active_record/associations.rb', line 1369 def parent_table_name @parent_table_name end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
1369 1370 1371 |
# File 'lib/active_record/associations.rb', line 1369 def reflection @reflection end |
Instance Method Details
#association_join ⇒ Object
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 |
# File 'lib/active_record/associations.rb', line 1408 def association_join join = case reflection.macro when :has_and_belongs_to_many " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_alias_for([:join_table], aliased_join_table_name), aliased_join_table_name, [:foreign_key] || reflection.active_record.to_s.classify.foreign_key, reflection.active_record.table_name, reflection.active_record.primary_key] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, aliased_table_name, klass.primary_key, aliased_join_table_name, [:association_foreign_key] || klass.table_name.classify.foreign_key ] when :has_many, :has_one case when reflection.macro == :has_many && reflection.[:through] through_conditions = through_reflection.[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.[:conditions]))}" : '' if through_reflection.[:as] # has_many :through against a polymorphic join polymorphic_foreign_key = through_reflection.[:as].to_s + '_id' polymorphic_foreign_type = through_reflection.[:as].to_s + '_type' " LEFT OUTER JOIN %s ON (%s.%s = %s.%s AND %s.%s = %s) " % [ table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, polymorphic_foreign_key, parent.aliased_table_name, parent.primary_key, aliased_join_table_name, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias, aliased_table_name, primary_key, aliased_join_table_name, [:foreign_key] || reflection.klass.to_s.classify.foreign_key ] else case source_reflection.macro when :belongs_to first_key = primary_key second_key = [:foreign_key] || klass.to_s.classify.foreign_key when :has_many first_key = through_reflection.klass.to_s.classify.foreign_key second_key = [:foreign_key] || primary_key end " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, through_reflection.primary_key_name, parent.aliased_table_name, parent.primary_key] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, aliased_table_name, first_key, aliased_join_table_name, second_key ] end when reflection.macro == :has_many && reflection.[:as] " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s" % [ table_name_and_alias, aliased_table_name, "#{reflection.[:as]}_id", parent.aliased_table_name, parent.primary_key, aliased_table_name, "#{reflection.[:as]}_type", klass.quote(parent.active_record.base_class.name) ] else foreign_key = [:foreign_key] || case reflection.macro when :has_many then reflection.active_record.to_s.classify when :has_one then reflection.active_record.to_s end.foreign_key " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, aliased_table_name, foreign_key, parent.aliased_table_name, parent.primary_key ] end when :belongs_to " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, aliased_table_name, reflection.klass.primary_key, parent.aliased_table_name, [:foreign_key] || klass.to_s.foreign_key ] else "" end || '' join << %(AND %s.%s = %s ) % [ aliased_table_name, reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), klass.quote(klass.name)] unless klass.descends_from_active_record? join << "AND #{interpolate_sql(sanitize_sql(reflection.[:conditions]))} " if reflection.[:conditions] join end |