Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 75 lines (59 sloc) 2.092 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
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
12 def insert_record(record, validate = true)
13 return if record.new_record? && !record.save(:validate => validate)
c1f833d @miloops habtm delete method integrated with ARel.
miloops authored
14
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflecti…
jonleighton authored
15 if options[:insert_sql]
16 owner.connection.insert(interpolate(options[:insert_sql], record))
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
17 else
18 stmt = join_table.compile_insert(
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
19 join_table[reflection.foreign_key] => owner.id,
20 join_table[reflection.association_foreign_key] => record.id
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
21 )
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
22
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
23 owner.connection.insert stmt.to_sql
db045db @dhh Initial
dhh authored
24 end
800b899 @jeremy Remove deprecated push_with_attributes.
jeremy authored
25
1644663 @jonleighton Split AssociationProxy into an Association class (and subclasses) whi…
jonleighton authored
26 record
27 end
28
29 def association_scope
30 super.joins(construct_joins)
31 end
15601c5 @jonleighton Let's be less blasé about method visibility on association proxies
jonleighton authored
32
33 private
34
35 def count_records
36 load_target.size
37 end
38
e62b576 @jonleighton Refactor the implementations of AssociatioCollection#delete and #dest…
jonleighton authored
39 def delete_records(records, method)
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflecti…
jonleighton authored
40 if sql = options[:delete_sql]
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
41 records.each { |record| owner.connection.delete(interpolate(sql, record)) }
db045db @dhh Initial
dhh authored
42 else
8bdc191 @tenderlove we have a method for this, so let's use it
tenderlove authored
43 relation = join_table
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
44 stmt = relation.where(relation[reflection.foreign_key].eq(owner.id).
45 and(relation[reflection.association_foreign_key].in(records.map { |x| x.id }.compact))
9a3e29e @tenderlove remove calls to deprecated methods
tenderlove authored
46 ).compile_delete
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
47 owner.connection.delete stmt.to_sql
db045db @dhh Initial
dhh authored
48 end
49 end
a3bd62e @miloops Remove explicit return.
miloops authored
50
b82fab2 @jonleighton Refactoring: replace the mix of variables like @finder_sql, @counter_…
jonleighton authored
51 def construct_joins
e468a62 @tenderlove use arel ast construction rather than generating strings
tenderlove authored
52 right = join_table
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
53 left = reflection.klass.arel_table
e468a62 @tenderlove use arel ast construction rather than generating strings
tenderlove authored
54
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
55 condition = left[reflection.klass.primary_key].eq(
56 right[reflection.association_foreign_key])
e468a62 @tenderlove use arel ast construction rather than generating strings
tenderlove authored
57
58 right.create_join(right, right.create_on(condition))
b82fab2 @jonleighton Refactoring: replace the mix of variables like @finder_sql, @counter_…
jonleighton authored
59 end
800b899 @jeremy Remove deprecated push_with_attributes.
jeremy authored
60
e8ada11 @jonleighton Associations: DRY up the code which is generating conditions, and mak…
jonleighton authored
61 def construct_owner_conditions
62 super(join_table)
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
63 end
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored
64
770e689 @jonleighton Construct an actual ActiveRecord::Relation object for the association…
jonleighton authored
65 def select_value
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than di…
jonleighton authored
66 super || reflection.klass.arel_table[Arel.star]
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored
67 end
a72c1ec @jeremy Refactor association create and build so before & after callbacks beh…
jeremy authored
68
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name a…
jonleighton authored
69 def invertible_for?(record)
70 false
71 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
72 end
db045db @dhh Initial
dhh authored
73 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming…
dhh authored
74 end
Something went wrong with that request. Please try again.