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.
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 |
# File 'lib/active_record/associations.rb', line 1377 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.
1375 1376 1377 |
# File 'lib/active_record/associations.rb', line 1375 def joins @joins end |
#reflections ⇒ Object (readonly)
Returns the value of attribute reflections.
1375 1376 1377 |
# File 'lib/active_record/associations.rb', line 1375 def reflections @reflections end |
#table_aliases ⇒ Object (readonly)
Returns the value of attribute table_aliases.
1375 1376 1377 |
# File 'lib/active_record/associations.rb', line 1375 def table_aliases @table_aliases end |
Instance Method Details
#instantiate(rows) ⇒ Object
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 |
# File 'lib/active_record/associations.rb', line 1396 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
1388 1389 1390 |
# File 'lib/active_record/associations.rb', line 1388 def join_associations @joins[1..-1].to_a end |
#join_base ⇒ Object
1392 1393 1394 |
# File 'lib/active_record/associations.rb', line 1392 def join_base @joins[0] end |
#remove_duplicate_results!(base, records, associations) ⇒ 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 |
# File 'lib/active_record/associations.rb', line 1408 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 |