Class: ActiveRecord::Associations::HasManyAssociation

Inherits:
AssociationCollection show all
Defined in:
lib/active_record/associations/has_many_association.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from AssociationCollection

#<<, #create, #delete, #destroy_all, #empty?, #length, #reload, #replace, #reset, #size, #to_ary, #uniq

Methods inherited from AssociationProxy

#loaded?, #method_missing, #proxy_respond_to?, #respond_to?

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, options)
  super
  @conditions = sanitize_sql(options[: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
# File 'lib/active_record/associations/has_many_association.rb', line 11

def build(attributes = {})
  load_target
  record = @association_class.new(attributes)
  record[@association_class_primary_key_name] = @owner.id unless @owner.new_record?
  @target << record
  record
end

#clearObject

Removes all records from this association. Returns self so method calls may be chained.



75
76
77
78
79
# File 'lib/active_record/associations/has_many_association.rb', line 75

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.



31
32
33
34
35
36
37
38
39
# File 'lib/active_record/associations/has_many_association.rb', line 31

def count(runtime_conditions = nil)
  if @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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_record/associations/has_many_association.rb', line 46

def find(*args)
  # Return an Array if multiple ids are given.
  expects_array = args.first.kind_of?(Array)

  ids = args.flatten.compact.uniq

  # If no ids given, raise RecordNotFound.
  if ids.empty?
    raise RecordNotFound, "Couldn't find #{@association_class.name} without an ID"

  # If using a custom finder_sql, scan the entire collection.
  elsif @options[:finder_sql]
    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

  # Otherwise, delegate to association class with conditions.
  else
    args << { :conditions => "#{@association_class_primary_key_name} = #{@owner.quoted_id} #{@conditions ? " AND " + @conditions : ""}" }
    @association_class.find(*args)
  end
end

#find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/associations/has_many_association.rb', line 19

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

Find the first associated record. All arguments are optional.



42
43
44
# File 'lib/active_record/associations/has_many_association.rb', line 42

def find_first(conditions = nil, orderings = nil)
  find_all(conditions, orderings, 1).first
end