Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 79 lines (62 sloc) 2.24 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
4 class HasAndBelongsToManyAssociation < AssociationCollection #: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
9bc75fd @lifo Remove duplicate code from associations. [Pratik]
lifo authored
12 protected
c1f833d @miloops habtm delete method integrated with ARel.
miloops authored
13
5cda000 @dhh Fixed that autosave should validate associations even if master is in…
dhh authored
14 def insert_record(record, force = true, validate = true)
e444439 @josevalim Partialy revert f1c13b0dd7b22b5f6289ca1a09f1d7a8c7c8584b
josevalim authored
15 if record.new_record?
379c022 @jonleighton Specify insert_record with NotImplementedError in AssociationCollecti…
jonleighton authored
16 return false unless save_record(record, force, validate)
a2f26b9 @dhh Fixed that adding a record to a has_and_belongs_to collection would a…
dhh authored
17 end
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
18
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
19 if @reflection.options[:insert_sql]
4979448 @technoweenie Ensure that modifying has_and_belongs_to_many actions clear the query…
technoweenie authored
20 @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
db045db @dhh Initial
dhh authored
21 else
d152171 @jonleighton Removed support for accessing attributes on a has_and_belongs_to_many…
jonleighton authored
22 stmt = join_table.compile_insert(
23 join_table[@reflection.foreign_key] => @owner.id,
24 join_table[@reflection.association_foreign_key] => record.id
25 )
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
26
1563351 @tenderlove removing some calls to insert on arel
tenderlove authored
27 @owner.connection.insert stmt.to_sql
db045db @dhh Initial
dhh authored
28 end
800b899 @jeremy Remove deprecated push_with_attributes.
jeremy authored
29
a3bd62e @miloops Remove explicit return.
miloops authored
30 true
db045db @dhh Initial
dhh authored
31 end
800b899 @jeremy Remove deprecated push_with_attributes.
jeremy authored
32
15601c5 @jonleighton Let's be less blasé about method visibility on association proxies
jonleighton authored
33 def association_scope
34 super.joins(construct_joins)
35 end
36
37 private
38
39 def count_records
40 load_target.size
41 end
42
db045db @dhh Initial
dhh authored
43 def delete_records(records)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
44 if sql = @reflection.options[:delete_sql]
4979448 @technoweenie Ensure that modifying has_and_belongs_to_many actions clear the query…
technoweenie authored
45 records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) }
db045db @dhh Initial
dhh authored
46 else
8bdc191 @tenderlove we have a method for this, so let's use it
tenderlove authored
47 relation = join_table
1267598 @jonleighton Rename AssociationReflection#primary_key_name to foreign_key, since t…
jonleighton authored
48 stmt = relation.where(relation[@reflection.foreign_key].eq(@owner.id).
79ef26c @tenderlove do not pass nil values to arel
tenderlove authored
49 and(relation[@reflection.association_foreign_key].in(records.map { |x| x.id }.compact))
9a3e29e @tenderlove remove calls to deprecated methods
tenderlove authored
50 ).compile_delete
51 @owner.connection.delete stmt.to_sql
db045db @dhh Initial
dhh authored
52 end
53 end
a3bd62e @miloops Remove explicit return.
miloops authored
54
b82fab2 @jonleighton Refactoring: replace the mix of variables like @finder_sql, @counter_…
jonleighton authored
55 def construct_joins
e468a62 @tenderlove use arel ast construction rather than generating strings
tenderlove authored
56 right = join_table
57 left = @reflection.klass.arel_table
58
59 condition = left[@reflection.klass.primary_key].eq(
60 right[@reflection.association_foreign_key])
61
62 right.create_join(right, right.create_on(condition))
b82fab2 @jonleighton Refactoring: replace the mix of variables like @finder_sql, @counter_…
jonleighton authored
63 end
800b899 @jeremy Remove deprecated push_with_attributes.
jeremy authored
64
e8ada11 @jonleighton Associations: DRY up the code which is generating conditions, and mak…
jonleighton authored
65 def construct_owner_conditions
66 super(join_table)
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
67 end
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored
68
770e689 @jonleighton Construct an actual ActiveRecord::Relation object for the association…
jonleighton authored
69 def select_value
d152171 @jonleighton Removed support for accessing attributes on a has_and_belongs_to_many…
jonleighton authored
70 super || @reflection.klass.arel_table[Arel.star]
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored
71 end
a72c1ec @jeremy Refactor association create and build so before & after callbacks beh…
jeremy authored
72
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name a…
jonleighton authored
73 def invertible_for?(record)
74 false
75 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
76 end
db045db @dhh Initial
dhh authored
77 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming…
dhh authored
78 end
Something went wrong with that request. Please try again.