Class: ActiveRecord::Associations::HasOneThroughAssociation

Inherits:
HasManyThroughAssociation show all
Defined in:
activerecord/lib/active_record/associations/has_one_through_association.rb

Instance Method Summary (collapse)

Methods inherited from HasManyThroughAssociation

#create, #create!, #size

Methods inherited from AssociationCollection

#<<, #any?, #build, #clear, #count, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #first, #include?, #initialize, #last, #length, #proxy_respond_to?, #replace, #reset, #size, #sum, #to_ary, #transaction, #uniq

Methods inherited from AssociationProxy

#===, #aliased_table_name, #conditions, #initialize, #inspect, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_respond_to?, #proxy_target, #reload, #reset, #respond_to?, #send, #target, #target=

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::AssociationCollection

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveRecord::Associations::AssociationCollection

Instance Method Details

- (Object) create_through_record(new_value)

nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'activerecord/lib/active_record/associations/has_one_through_association.rb', line 5

def create_through_record(new_value) #nodoc:
  klass = @reflection.through_reflection.klass

  current_object = @owner.send(@reflection.through_reflection.name)

  if current_object
    new_value ? current_object.update_attributes(construct_join_attributes(new_value)) : current_object.destroy
  elsif new_value
    if @owner.new_record?
      self.target = new_value
      through_association = @owner.send(:association_instance_get, @reflection.through_reflection.name)
      through_association.build(construct_join_attributes(new_value))
    else
      @owner.send(@reflection.through_reflection.name, klass.create(construct_join_attributes(new_value)))
    end
  end
end