Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 66 lines (54 sloc) 1.834 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)
4649294 @pixeltrix Improve the derivation of HABTM assocation join table names
pixeltrix authored
8 @join_table = Arel::Table.new(reflection.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
7f3b475 @jonleighton Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."
jonleighton authored
21 if options[:insert_sql]
22 owner.connection.insert(interpolate(options[:insert_sql], record))
23 else
24 stmt = join_table.compile_insert(
25 join_table[reflection.foreign_key] => owner.id,
26 join_table[reflection.association_foreign_key] => record.id
27 )
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
28
7f3b475 @jonleighton Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."
jonleighton authored
29 owner.connection.insert stmt
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
15601c5 @jonleighton Let's be less blasé about method visibility on association proxies
jonleighton authored
35 private
36
37 def count_records
38 load_target.size
39 end
40
e62b576 @jonleighton Refactor the implementations of AssociatioCollection#delete and #dest…
jonleighton authored
41 def delete_records(records, method)
7f3b475 @jonleighton Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."
jonleighton authored
42 if sql = options[:delete_sql]
43 records = load_target if records == :all
44 records.each { |record| owner.connection.delete(interpolate(sql, record)) }
45 else
46 relation = join_table
47 condition = relation[reflection.foreign_key].eq(owner.id)
3803fcc @jonleighton Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql.
jonleighton authored
48
7f3b475 @jonleighton Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."
jonleighton authored
49 unless records == :all
50 condition = condition.and(
51 relation[reflection.association_foreign_key]
52 .in(records.map { |x| x.id }.compact)
53 )
54 end
55
56 owner.connection.delete(relation.where(condition).compile_delete)
57 end
db045db @dhh Initial
dhh authored
58 end
a3bd62e @miloops Remove explicit return.
miloops authored
59
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name a…
jonleighton authored
60 def invertible_for?(record)
61 false
62 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
63 end
db045db @dhh Initial
dhh authored
64 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming…
dhh authored
65 end
Something went wrong with that request. Please try again.