Class: ActiveRecord::Associations::JoinDependency
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::JoinDependency
- Defined in:
- lib/active_record/associations/join_dependency.rb,
lib/active_record/associations/join_dependency/join_base.rb,
lib/active_record/associations/join_dependency/join_part.rb,
lib/active_record/associations/join_dependency/join_association.rb
Overview
:nodoc:
Defined Under Namespace
Classes: JoinAssociation, JoinBase, JoinPart
Instance Attribute Summary collapse
-
#alias_tracker ⇒ Object
readonly
Returns the value of attribute alias_tracker.
-
#base_klass ⇒ Object
readonly
Returns the value of attribute base_klass.
-
#join_parts ⇒ Object
readonly
Returns the value of attribute join_parts.
-
#reflections ⇒ Object
readonly
Returns the value of attribute reflections.
Instance Method Summary collapse
- #columns ⇒ Object
- #graft(*associations) ⇒ Object
-
#initialize(base, associations, joins) ⇒ JoinDependency
constructor
base is the base class on which operation is taking place.
- #instantiate(rows) ⇒ Object
- #join_associations ⇒ Object
- #join_base ⇒ Object
Constructor Details
#initialize(base, associations, joins) ⇒ JoinDependency
base is the base class on which operation is taking place. associations is the list of associations which are joined using hash, symbol or array. joins is the list of all string join commnads and arel nodes.
Example :
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
If I execute `@physician.patients.to_a` then
base #=> Physician
associations #=> []
joins #=> [#<Arel::Nodes::InnerJoin: ...]
However if I execute `Physician.joins(:appointments).to_a` then
base #=> Physician
associations #=> [:appointments]
joins #=> []
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record/associations/join_dependency.rb', line 31 def initialize(base, associations, joins) @base_klass = base @table_joins = joins @join_parts = [JoinBase.new(base)] @associations = {} @reflections = [] @alias_tracker = AliasTracker.new(base.connection, joins) @alias_tracker.aliased_name_for(base.table_name) # Updates the count for base.table_name to 1 build(associations) end |
Instance Attribute Details
#alias_tracker ⇒ Object (readonly)
Returns the value of attribute alias_tracker.
8 9 10 |
# File 'lib/active_record/associations/join_dependency.rb', line 8 def alias_tracker @alias_tracker end |
#base_klass ⇒ Object (readonly)
Returns the value of attribute base_klass.
8 9 10 |
# File 'lib/active_record/associations/join_dependency.rb', line 8 def base_klass @base_klass end |
#join_parts ⇒ Object (readonly)
Returns the value of attribute join_parts.
8 9 10 |
# File 'lib/active_record/associations/join_dependency.rb', line 8 def join_parts @join_parts end |
#reflections ⇒ Object (readonly)
Returns the value of attribute reflections.
8 9 10 |
# File 'lib/active_record/associations/join_dependency.rb', line 8 def reflections @reflections end |
Instance Method Details
#columns ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/active_record/associations/join_dependency.rb', line 58 def columns join_parts.collect { |join_part| table = join_part.aliased_table join_part.column_names_with_alias.collect{ |column_name, aliased_name| table[column_name].as Arel.sql(aliased_name) } }.flatten end |
#graft(*associations) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/active_record/associations/join_dependency.rb', line 42 def graft(*associations) associations.each do |association| join_associations.detect {|a| association == a} || build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type) end self end |
#instantiate(rows) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_record/associations/join_dependency.rb', line 67 def instantiate(rows) primary_key = join_base.aliased_primary_key parents = {} records = rows.map { |model| primary_id = model[primary_key] parent = parents[primary_id] ||= join_base.instantiate(model) construct(parent, @associations, join_associations, model) parent }.uniq remove_duplicate_results!(base_klass, records, @associations) records end |
#join_associations ⇒ Object
50 51 52 |
# File 'lib/active_record/associations/join_dependency.rb', line 50 def join_associations join_parts.last(join_parts.length - 1) end |
#join_base ⇒ Object
54 55 56 |
# File 'lib/active_record/associations/join_dependency.rb', line 54 def join_base join_parts.first end |