Class: ActiveRecord::Associations::HasManyThroughAssociation
- Inherits:
-
HasManyAssociation
- Object
- AssociationProxy
- AssociationCollection
- HasManyAssociation
- ActiveRecord::Associations::HasManyThroughAssociation
- Defined in:
- lib/active_record/associations/has_many_through_association.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
- #create(attrs = nil) ⇒ Object
- #create!(attrs = nil) ⇒ Object
-
#initialize(owner, reflection) ⇒ HasManyThroughAssociation
constructor
A new instance of HasManyThroughAssociation.
-
#size ⇒ Object
Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn’t been loaded and calling collection.size if it has.
Methods inherited from AssociationCollection
#<<, #any?, #build, #clear, #count, #delete, #delete_all, #destroy_all, #empty?, #find, #first, #include?, #last, #length, #proxy_respond_to?, #replace, #reset, #sum, #to_ary, #transaction, #uniq
Methods inherited from AssociationProxy
#===, #aliased_table_name, #inspect, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_respond_to?, #proxy_target, #reload, #reset, #respond_to?, #send, #target, #target=
Constructor Details
#initialize(owner, reflection) ⇒ HasManyThroughAssociation
Returns a new instance of HasManyThroughAssociation.
4 5 6 7 |
# File 'lib/active_record/associations/has_many_through_association.rb', line 4 def initialize(owner, reflection) reflection.check_validity! super end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveRecord::Associations::AssociationCollection
Instance Method Details
#create(attrs = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/active_record/associations/has_many_through_association.rb', line 18 def create(attrs = nil) transaction do self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association) object end end |
#create!(attrs = nil) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/active_record/associations/has_many_through_association.rb', line 11 def create!(attrs = nil) transaction do self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!) object end end |
#size ⇒ Object
Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn’t been loaded and calling collection.size if it has. If it’s more likely than not that the collection does have a size larger than zero and you need to fetch that collection afterwards, it’ll take one less SELECT query if you use length.
28 29 30 31 32 |
# File 'lib/active_record/associations/has_many_through_association.rb', line 28 def size return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter? return @target.size if loaded? return count end |