Class: ActiveRecord::Associations::ClassMethods::JoinDependency
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::ClassMethods::JoinDependency
- Defined in:
- lib/active_record/associations.rb
Overview
:nodoc:
Direct Known Subclasses
Defined Under Namespace
Classes: JoinAssociation, JoinBase
Instance Attribute Summary collapse
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#reflections ⇒ Object
readonly
Returns the value of attribute reflections.
-
#table_aliases ⇒ Object
readonly
Returns the value of attribute table_aliases.
Instance Method Summary collapse
-
#initialize(base, associations, joins) ⇒ JoinDependency
constructor
A new instance of JoinDependency.
- #instantiate(rows) ⇒ Object
- #join_associations ⇒ Object
- #join_base ⇒ Object
- #remove_duplicate_results!(base, records, associations) ⇒ Object
Constructor Details
#initialize(base, associations, joins) ⇒ JoinDependency
Returns a new instance of JoinDependency.
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 |
# File 'lib/active_record/associations.rb', line 1416 def initialize(base, associations, joins) @joins = [JoinBase.new(base, joins)] @associations = associations @reflections = [] @base_records_hash = {} @base_records_in_order = [] @table_aliases = Hash.new { |aliases, table| aliases[table] = 0 } @table_aliases[base.table_name] = 1 build(associations) end |
Instance Attribute Details
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
1414 1415 1416 |
# File 'lib/active_record/associations.rb', line 1414 def joins @joins end |
#reflections ⇒ Object (readonly)
Returns the value of attribute reflections.
1414 1415 1416 |
# File 'lib/active_record/associations.rb', line 1414 def reflections @reflections end |
#table_aliases ⇒ Object (readonly)
Returns the value of attribute table_aliases.
1414 1415 1416 |
# File 'lib/active_record/associations.rb', line 1414 def table_aliases @table_aliases end |
Instance Method Details
#instantiate(rows) ⇒ Object
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 |
# File 'lib/active_record/associations.rb', line 1435 def instantiate(rows) rows.each_with_index do |row, i| primary_id = join_base.record_id(row) unless @base_records_hash[primary_id] @base_records_in_order << (@base_records_hash[primary_id] = join_base.instantiate(row)) end construct(@base_records_hash[primary_id], @associations, join_associations.dup, row) end remove_duplicate_results!(join_base.active_record, @base_records_in_order, @associations) return @base_records_in_order end |
#join_associations ⇒ Object
1427 1428 1429 |
# File 'lib/active_record/associations.rb', line 1427 def join_associations @joins[1..-1].to_a end |
#join_base ⇒ Object
1431 1432 1433 |
# File 'lib/active_record/associations.rb', line 1431 def join_base @joins[0] end |
#remove_duplicate_results!(base, records, associations) ⇒ Object
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 |
# File 'lib/active_record/associations.rb', line 1447 def remove_duplicate_results!(base, records, associations) case associations when Symbol, String reflection = base.reflections[associations] if reflection && [:has_many, :has_and_belongs_to_many].include?(reflection.macro) records.each { |record| record.send(reflection.name).target.uniq! } end when Array associations.each do |association| remove_duplicate_results!(base, records, association) end when Hash associations.keys.each do |name| reflection = base.reflections[name] is_collection = [:has_many, :has_and_belongs_to_many].include?(reflection.macro) parent_records = records.map do |record| next unless record.send(reflection.name) is_collection ? record.send(reflection.name).target.uniq! : record.send(reflection.name) end.flatten.compact remove_duplicate_results!(reflection.class_name.constantize, parent_records, associations[name]) unless parent_records.empty? end end end |