Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 59 lines (49 sloc) 1.667 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) which ...
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 that ...
jonleighton authored
11
dfaad4f @jonleighton 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 @miloops habtm delete method integrated with ARel.
miloops authored
20
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflection....
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) which ...
jonleighton authored
23 else
24 stmt = join_table.compile_insert(
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than direc...
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) which ...
jonleighton authored
27 )
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attribut...
dhh authored
28
7db90aa @jonleighton Make it the responsibility of the connection to hold onto an ARel visito...
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) which ...
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 #destroy...
jonleighton authored
41 def delete_records(records, method)
32eef69 @jonleighton Delegate Association#options to the reflection, and replace 'reflection....
jonleighton authored
42 if sql = options[:delete_sql]
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than direc...
jonleighton authored
43 records.each { |record| owner.connection.delete(interpolate(sql, record)) }
db045db @dhh Initial
dhh authored
44 else
8bdc191 @tenderlove we have a method for this, so let's use it
tenderlove authored
45 relation = join_table
1d85a73 @jonleighton Associations - where possible, call attributes methods rather than direc...
jonleighton authored
46 stmt = relation.where(relation[reflection.foreign_key].eq(owner.id).
47 and(relation[reflection.association_foreign_key].in(records.map { |x| x.id }.compact))
9a3e29e @tenderlove remove calls to deprecated methods
tenderlove authored
48 ).compile_delete
7db90aa @jonleighton Make it the responsibility of the connection to hold onto an ARel visito...
jonleighton authored
49 owner.connection.delete stmt
db045db @dhh Initial
dhh authored
50 end
51 end
a3bd62e @miloops Remove explicit return.
miloops authored
52
9f5c18c @jonleighton Refactor we_can_set_the_inverse_on_this? to use a less bizarre name amon...
jonleighton authored
53 def invertible_for?(record)
54 false
55 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
56 end
db045db @dhh Initial
dhh authored
57 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming co...
dhh authored
58 end
Something went wrong with that request. Please try again.