Class: ActiveRecord::Associations::Preloader::Association
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Preloader::Association
- Defined in:
- lib/active_record/associations/preloader/association.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#owners ⇒ Object
readonly
Returns the value of attribute owners.
-
#preload_options ⇒ Object
readonly
Returns the value of attribute preload_options.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Instance Method Summary collapse
-
#association_key ⇒ Object
This is overridden by HABTM as the condition should be on the foreign_key column in the join table.
-
#association_key_name ⇒ Object
The name of the key on the associated records.
-
#initialize(klass, owners, reflection, preload_options) ⇒ Association
constructor
A new instance of Association.
- #options ⇒ Object
-
#owner_key_name ⇒ Object
The name of the key on the model which declares the association.
-
#owners_by_key ⇒ Object
We’re converting to a string here because postgres will return the aliased association key in a habtm as a string (for whatever reason).
- #preload ⇒ Object
- #records_for(ids) ⇒ Object
- #run ⇒ Object
- #scoped ⇒ Object
- #table ⇒ Object
Constructor Details
#initialize(klass, owners, reflection, preload_options) ⇒ Association
Returns a new instance of Association.
7 8 9 10 11 12 13 14 15 |
# File 'lib/active_record/associations/preloader/association.rb', line 7 def initialize(klass, owners, reflection, ) @klass = klass @owners = owners @reflection = reflection @preload_options = || {} @model = owners.first && owners.first.class @scoped = nil @owners_by_key = nil end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/active_record/associations/preloader/association.rb', line 5 def klass @klass end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/active_record/associations/preloader/association.rb', line 5 def model @model end |
#owners ⇒ Object (readonly)
Returns the value of attribute owners.
5 6 7 |
# File 'lib/active_record/associations/preloader/association.rb', line 5 def owners @owners end |
#preload_options ⇒ Object (readonly)
Returns the value of attribute preload_options.
5 6 7 |
# File 'lib/active_record/associations/preloader/association.rb', line 5 def @preload_options end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
5 6 7 |
# File 'lib/active_record/associations/preloader/association.rb', line 5 def reflection @reflection end |
Instance Method Details
#association_key ⇒ Object
This is overridden by HABTM as the condition should be on the foreign_key column in the join table
46 47 48 |
# File 'lib/active_record/associations/preloader/association.rb', line 46 def association_key table[association_key_name] end |
#association_key_name ⇒ Object
The name of the key on the associated records
40 41 42 |
# File 'lib/active_record/associations/preloader/association.rb', line 40 def association_key_name raise NotImplementedError end |
#options ⇒ Object
64 65 66 |
# File 'lib/active_record/associations/preloader/association.rb', line 64 def reflection. end |
#owner_key_name ⇒ Object
The name of the key on the model which declares the association
51 52 53 |
# File 'lib/active_record/associations/preloader/association.rb', line 51 def owner_key_name raise NotImplementedError end |
#owners_by_key ⇒ Object
We’re converting to a string here because postgres will return the aliased association key in a habtm as a string (for whatever reason)
57 58 59 60 61 62 |
# File 'lib/active_record/associations/preloader/association.rb', line 57 def owners_by_key @owners_by_key ||= owners.group_by do |owner| key = owner[owner_key_name] key && key.to_s end end |
#preload ⇒ Object
23 24 25 |
# File 'lib/active_record/associations/preloader/association.rb', line 23 def preload raise NotImplementedError end |
#records_for(ids) ⇒ Object
31 32 33 |
# File 'lib/active_record/associations/preloader/association.rb', line 31 def records_for(ids) scoped.where(association_key.in(ids)) end |
#run ⇒ Object
17 18 19 20 21 |
# File 'lib/active_record/associations/preloader/association.rb', line 17 def run unless owners.first.association(reflection.name).loaded? preload end end |
#scoped ⇒ Object
27 28 29 |
# File 'lib/active_record/associations/preloader/association.rb', line 27 def scoped @scoped ||= build_scope end |
#table ⇒ Object
35 36 37 |
# File 'lib/active_record/associations/preloader/association.rb', line 35 def table klass.arel_table end |