Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 66 lines (54 sloc) 1.834 kb
db045db David Heinemeier Hansson Initial
dhh authored
1 module ActiveRecord
fde9504 Rizwan Reza Adds title to activerecord/lib/active_record/associations/*
rizwanreza authored
2 # = Active Record Has And Belongs To Many Association
db045db David Heinemeier Hansson Initial
dhh authored
3 module Associations
1644663 Jon Leighton Split AssociationProxy into an Association class (and subclasses) which ...
jonleighton authored
4 class HasAndBelongsToManyAssociation < CollectionAssociation #:nodoc:
90171ad Aaron Patterson avoid creating so many Arel::Table objects
tenderlove authored
5 attr_reader :join_table
6
7 def initialize(owner, reflection)
4649294 Andrew White Improve the derivation of HABTM assocation join table names
pixeltrix authored
8 @join_table = Arel::Table.new(reflection.join_table)
90171ad Aaron Patterson avoid creating so many Arel::Table objects
tenderlove authored
9 super
10 end
3103296 Jon Leighton Let AssociationCollection#find use #scoped to do its finding. Note that ...
jonleighton authored
11
dfaad4f Jon Leighton Only save the record once when calling create! on a collection associati...
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 Emilio Tagua habtm delete method integrated with ARel.
miloops authored
20
7f3b475 Jon Leighton 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 David Heinemeier Hansson Added that has_and_belongs_to_many associations with additional attribut...
dhh authored
28
7f3b475 Jon Leighton Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."
jonleighton authored
29 owner.connection.insert stmt
30 end
800b899 Jeremy Kemper Remove deprecated push_with_attributes.
jeremy authored
31
1644663 Jon Leighton Split AssociationProxy into an Association class (and subclasses) which ...
jonleighton authored
32 record
33 end
34
15601c5 Jon Leighton 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 Jon Leighton Refactor the implementations of AssociatioCollection#delete and #destroy...
jonleighton authored
41 def delete_records(records, method)
7f3b475 Jon Leighton 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 Jon Leighton Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql.
jonleighton authored
48
7f3b475 Jon Leighton 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 David Heinemeier Hansson Initial
dhh authored
58 end
a3bd62e Emilio Tagua Remove explicit return.
miloops authored
59
9f5c18c Jon Leighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name amon...
jonleighton authored
60 def invertible_for?(record)
61 false
62 end
823554e David Heinemeier Hansson Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
63 end
db045db David Heinemeier Hansson Initial
dhh authored
64 end
68d1056 David Heinemeier Hansson Fixed that has_and_belongs_to_many would generate bad sql when naming co...
dhh authored
65 end
Something went wrong with that request. Please try again.