Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 64 lines (53 sloc) 1.884 kb
db045db @dhh Initial
dhh authored
1 module ActiveRecord
fde9504 @rizwanreza Adds title to activerecord/lib/active_record/associations/*
rizwanreza authored
2 # = Active Record Has And Belongs To Many Association
db045db @dhh Initial
dhh authored
3 module Associations
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
4 class HasAndBelongsToManyAssociation < CollectionAssociation #:nodoc:
90171ad @tenderlove avoid creating so many Arel::Table objects
tenderlove authored
5 attr_reader :join_table
6
7 def initialize(owner, reflection)
c5e912a @jonleighton @join_table_name is no longer used
jonleighton authored
8 @join_table = Arel::Table.new(reflection.options[:join_table])
90171ad @tenderlove avoid creating so many Arel::Table objects
tenderlove authored
9 super
10 end
3103296 @jonleighton Let AssociationCollection#find use #scoped to do its finding. Note th…
jonleighton authored
11
dfaad4f @jonleighton Only save the record once when calling create! on a collection associ…
jonleighton authored
12 def insert_record(record, validate = true, raise = false)
13 if record.new_record?
14 if raise
15 record.save!(:validate => validate)
16 else
17 return unless record.save(:validate => validate)
18 end
19 end
c1f833d @miloops habtm delete method integrated with ARel.
miloops authored
20
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflecti…
jonleighton authored
21 if options[:insert_sql]
22 owner.connection.insert(interpolate(options[:insert_sql], record))
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
23 else
24 stmt = join_table.compile_insert(
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
25 join_table[reflection.foreign_key] => owner.id,
26 join_table[reflection.association_foreign_key] => record.id
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
27 )
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
28
7db90aa @jonleighton Make it the responsibility of the connection to hold onto an ARel vis…
jonleighton authored
29 owner.connection.insert stmt
db045db @dhh Initial
dhh authored
30 end
800b899 @jeremy Remove deprecated push_with_attributes.
jeremy authored
31
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
32 record
33 end
34
889e8be @jonleighton Fix #3672 again (dependent: delete_all perf)
jonleighton authored
35 # ActiveRecord::Relation#delete_all needs to support joins before we can use a
36 # SQL-only implementation.
37 alias delete_all_on_destroy delete_all
38
15601c5 @jonleighton Let's be less blasé about method visibility on association proxies
jonleighton authored
39 private
40
41 def count_records
42 load_target.size
43 end
44
e62b576 @jonleighton Refactor the implementations of AssociatioCollection#delete and #dest…
jonleighton authored
45 def delete_records(records, method)
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflecti…
jonleighton authored
46 if sql = options[:delete_sql]
855fabc @jonleighton fix #delete_all with habtm with :delete_sql
jonleighton authored
47 records = load_target if records == :all
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
48 records.each { |record| owner.connection.delete(interpolate(sql, record)) }
db045db @dhh Initial
dhh authored
49 else
959fb8e @jonleighton Revert "Perf: Don't load the association for #delete_all."
jonleighton authored
50 relation = join_table
51 stmt = relation.where(relation[reflection.foreign_key].eq(owner.id).
52 and(relation[reflection.association_foreign_key].in(records.map { |x| x.id }.compact))
53 ).compile_delete
54 owner.connection.delete stmt
db045db @dhh Initial
dhh authored
55 end
56 end
a3bd62e @miloops Remove explicit return.
miloops authored
57
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name a…
jonleighton authored
58 def invertible_for?(record)
59 false
60 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
61 end
db045db @dhh Initial
dhh authored
62 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming…
dhh authored
63 end
Something went wrong with that request. Please try again.