Class: ActiveRecord::Associations::HasManyAssociation
- Inherits:
-
AssociationCollection
- Object
- AssociationProxy
- AssociationCollection
- ActiveRecord::Associations::HasManyAssociation
- Defined in:
- lib/active_record/associations/has_many_association.rb
Overview
:nodoc:
Instance Method Summary collapse
- #build(attributes = {}) ⇒ Object
-
#clear ⇒ Object
Removes all records from this association.
-
#count(runtime_conditions = nil) ⇒ Object
Count the number of associated records.
- #find(*args) ⇒ Object
-
#find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) ⇒ Object
DEPRECATED.
-
#find_first(conditions = nil, orderings = nil) ⇒ Object
DEPRECATED.
-
#initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) ⇒ HasManyAssociation
constructor
A new instance of HasManyAssociation.
Methods inherited from AssociationCollection
#<<, #create, #delete, #destroy_all, #empty?, #length, #replace, #reset, #size, #to_ary, #uniq
Methods inherited from AssociationProxy
#loaded, #loaded?, #method_missing, #proxy_respond_to?, #reload, #respond_to?, #target, #target=
Constructor Details
#initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) ⇒ HasManyAssociation
Returns a new instance of HasManyAssociation.
4 5 6 7 8 9 |
# File 'lib/active_record/associations/has_many_association.rb', line 4 def initialize(owner, association_name, association_class_name, association_class_primary_key_name, ) super @conditions = sanitize_sql([:conditions]) construct_sql end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveRecord::Associations::AssociationProxy
Instance Method Details
#build(attributes = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/active_record/associations/has_many_association.rb', line 11 def build(attributes = {}) if attributes.is_a?(Array) attributes.collect { |attr| create(attr) } else load_target record = @association_class.new(attributes) record[@association_class_primary_key_name] = @owner.id unless @owner.new_record? @target << record record end end |
#clear ⇒ Object
Removes all records from this association. Returns self
so method calls may be chained.
89 90 91 92 93 |
# File 'lib/active_record/associations/has_many_association.rb', line 89 def clear @association_class.update_all("#{@association_class_primary_key_name} = NULL", "#{@association_class_primary_key_name} = #{@owner.quoted_id}") @target = [] self end |
#count(runtime_conditions = nil) ⇒ Object
Count the number of associated records. All arguments are optional.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_record/associations/has_many_association.rb', line 41 def count(runtime_conditions = nil) if @options[:counter_sql] @association_class.count_by_sql(@counter_sql) elsif @options[:finder_sql] @association_class.count_by_sql(@finder_sql) else sql = @finder_sql sql += " AND #{sanitize_sql(runtime_conditions)}" if runtime_conditions @association_class.count(sql) end end |
#find(*args) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_record/associations/has_many_association.rb', line 53 def find(*args) = Base.send(:extract_options_from_args!, args) # If using a custom finder_sql, scan the entire collection. if @options[:finder_sql] expects_array = args.first.kind_of?(Array) ids = args.flatten.compact.uniq if ids.size == 1 id = ids.first record = load_target.detect { |record| id == record.id } expects_array? ? [record] : record else load_target.select { |record| ids.include?(record.id) } end else conditions = "#{@finder_sql}" if sanitized_conditions = sanitize_sql([:conditions]) conditions << " AND #{sanitized_conditions}" end [:conditions] = conditions if [:order] && @options[:order] [:order] = "#{[:order]}, #{@options[:order]}" elsif @options[:order] [:order] = @options[:order] end # Pass through args exactly as we received them. args << @association_class.find(*args) end end |
#find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) ⇒ Object
DEPRECATED.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/active_record/associations/has_many_association.rb', line 24 def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) if @options[:finder_sql] records = @association_class.find_by_sql(@finder_sql) else sql = @finder_sql sql += " AND #{sanitize_sql(runtime_conditions)}" if runtime_conditions orderings ||= @options[:order] records = @association_class.find_all(sql, orderings, limit, joins) end end |
#find_first(conditions = nil, orderings = nil) ⇒ Object
DEPRECATED. Find the first associated record. All arguments are optional.
36 37 38 |
# File 'lib/active_record/associations/has_many_association.rb', line 36 def find_first(conditions = nil, orderings = nil) find_all(conditions, orderings, 1).first end |