Class: ActiveRecord::Associations::HasAndBelongsToManyAssociation

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

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from AssociationProxy

#reflection

Instance Method Summary collapse

Methods inherited from AssociationCollection

#<<, #clear, #delete, #delete_all, #destroy_all, #empty?, #length, #replace, #reset, #size, #sum, #to_ary, #uniq

Methods inherited from AssociationProxy

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

Constructor Details

#initialize(owner, reflection) ⇒ HasAndBelongsToManyAssociation

Returns a new instance of HasAndBelongsToManyAssociation.



4
5
6
7
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 4

def initialize(owner, reflection)
  super
  construct_sql
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



89
90
91
92
93
94
95
96
97
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 89

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(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
      @reflection.klass.send(method, *args, &block)
    end
  end
end

Instance Method Details

#build(attributes = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 9

def build(attributes = {})
  load_target
  record = @reflection.klass.new(attributes)
  @target << record
  record
end

#create(attributes = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 16

def create(attributes = {})
  # Can't use Base.create since the foreign key may be a protected attribute.
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr) }
  else
    record = build(attributes)
    insert_record(record) unless @owner.new_record?
    record
  end
end

#find(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 31

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

  # If using a custom finder_sql, scan the entire collection.
  if @reflection.options[:finder_sql]
    expects_array = args.first.kind_of?(Array)
    ids = args.flatten.compact.uniq

    if ids.size == 1
      id = ids.first.to_i
      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(options[:conditions])
      conditions << " AND (#{sanitized_conditions})"
    end

    options[:conditions] = conditions
    options[:joins]      = @join_sql
    options[:readonly]   = finding_with_ambigious_select?(options[:select])

    if options[:order] && @reflection.options[:order]
      options[:order] = "#{options[:order]}, #{@reflection.options[:order]}"
    elsif @reflection.options[:order]
      options[:order] = @reflection.options[:order]
    end

    merge_options_from_reflection!(options)

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

#find_firstObject



27
28
29
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 27

def find_first
  load_target.first
end

#push_with_attributes(record, join_attributes = {}) ⇒ Object Also known as: concat_with_attributes

Deprecated as of Rails 1.2. If your associations require attributes you should be using has_many :through



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_record/associations/has_and_belongs_to_many_association.rb', line 73

def push_with_attributes(record, join_attributes = {})
  raise_on_type_mismatch(record)
  join_attributes.each { |key, value| record[key.to_s] = value }

  callback(:before_add, record)
  insert_record(record) unless @owner.new_record?
  @target << record
  callback(:after_add, record)

  self
end