Class: ActiveRecord::Associations::HasManyThroughAssociation

Inherits:
AssociationProxy show all
Defined in:
lib/active_record/associations/has_many_through_association.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from AssociationProxy

#reflection

Instance Method Summary collapse

Methods inherited from AssociationProxy

#===, #aliased_table_name, #loaded, #loaded?, #proxy_respond_to?, #reload, #respond_to?, #target, #target=

Constructor Details

#initialize(owner, reflection) ⇒ HasManyThroughAssociation

Returns a new instance of HasManyThroughAssociation.



4
5
6
7
8
9
# File 'lib/active_record/associations/has_many_through_association.rb', line 4

def initialize(owner, reflection)
  super
  reflection.check_validity!
  @finder_sql = construct_conditions
  construct_sql
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



45
46
47
48
49
50
51
# File 'lib/active_record/associations/has_many_through_association.rb', line 45

def method_missing(method, *args, &block)
  if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
    super
  else
    @reflection.klass.with_scope(construct_scope) { @reflection.klass.send(method, *args, &block) }
  end
end

Instance Method Details

#find(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_record/associations/has_many_through_association.rb', line 12

def find(*args)
  options = Base.send(:extract_options_from_args!, args)

  conditions = "#{@finder_sql}"
  if sanitized_conditions = sanitize_sql(options[:conditions])
    conditions << " AND (#{sanitized_conditions})"
  end
  options[:conditions] = conditions

  if options[:order] && @reflection.options[:order]
    options[:order] = "#{options[:order]}, #{@reflection.options[:order]}"
  elsif @reflection.options[:order]
    options[:order] = @reflection.options[:order]
  end
  
  options[:select]  = construct_select(options[:select])
  options[:from]  ||= construct_from
  options[:joins]   = construct_joins(options[:joins])
  options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil?
  
  merge_options_from_reflection!(options)

  # Pass through args exactly as we received them.
  args << options
  @reflection.klass.find(*args)
end

#resetObject



39
40
41
42
# File 'lib/active_record/associations/has_many_through_association.rb', line 39

def reset
  @target = []
  @loaded = false
end