Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 170 lines (141 sloc) 6.378 kB
db045db @dhh Initial
dhh authored
1 module ActiveRecord
2 module Associations
3 class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc:
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
4 def initialize(owner, reflection)
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
5 super
6 construct_sql
db045db @dhh Initial
dhh authored
7 end
8
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
9 def build(attributes = {})
10 load_target
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
11 record = @reflection.klass.new(attributes)
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
12 @target << record
13 record
14 end
15
06075a9 @technoweenie Fix the has_and_belongs_to_many #create doesn't populate the join for…
technoweenie authored
16 def create(attributes = {})
17 # Can't use Base.create since the foreign key may be a protected attribute.
18 if attributes.is_a?(Array)
19 attributes.collect { |attr| create(attr) }
20 else
21 record = build(attributes)
85fbb22 @dhh Backed out of new_record? to new? transformation as it would screw up…
dhh authored
22 insert_record(record) unless @owner.new_record?
06075a9 @technoweenie Fix the has_and_belongs_to_many #create doesn't populate the join for…
technoweenie authored
23 record
24 end
25 end
26
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
27 def find_first
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
28 load_target.first
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
29 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
30
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
31 def find(*args)
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
32 options = Base.send(:extract_options_from_args!, args)
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
33
34 # If using a custom finder_sql, scan the entire collection.
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
35 if @reflection.options[:finder_sql]
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
36 expects_array = args.first.kind_of?(Array)
37 ids = args.flatten.compact.uniq
38
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
39 if ids.size == 1
4307d7e @dhh Fixed various problems with has_and_belongs_to_many when using custom…
dhh authored
40 id = ids.first.to_i
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
41 record = load_target.detect { |record| id == record.id }
4307d7e @dhh Fixed various problems with has_and_belongs_to_many when using custom…
dhh authored
42 expects_array ? [record] : record
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
43 else
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
44 load_target.select { |record| ids.include?(record.id) }
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored
45 end
db045db @dhh Initial
dhh authored
46 else
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
47 conditions = "#{@finder_sql}"
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
48
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
49 if sanitized_conditions = sanitize_sql(options[:conditions])
328ef3f @jeremy Parenthesize :conditions
jeremy authored
50 conditions << " AND (#{sanitized_conditions})"
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
51 end
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
52
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
53 options[:conditions] = conditions
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored
54 options[:joins] = @join_sql
55 options[:readonly] = finding_with_ambigious_select?(options[:select])
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
56
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
57 if options[:order] && @reflection.options[:order]
58 options[:order] = "#{options[:order]}, #{@reflection.options[:order]}"
59 elsif @reflection.options[:order]
60 options[:order] = @reflection.options[:order]
db045db @dhh Initial
dhh authored
61 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
62
e5d9ad3 @dhh Added option inheritance for find calls on has_and_belongs_to_many an…
dhh authored
63 merge_options_from_reflection!(options)
64
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
65 # Pass through args exactly as we received them.
66 args << options
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
67 @reflection.klass.find(*args)
db045db @dhh Initial
dhh authored
68 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
69 end
6ba4f4c @NZKoz Formally deprecate rich associations. [Koz]
NZKoz authored
70
71 # Deprecated as of Rails 1.2. If your associations require attributes
72 # you should be using has_many :through
db045db @dhh Initial
dhh authored
73 def push_with_attributes(record, join_attributes = {})
74 raise_on_type_mismatch(record)
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
75 join_attributes.each { |key, value| record[key.to_s] = value }
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
76
0e92f36 @dhh Added callbacks on push_with_attributes #1594 [Florian Weber]
dhh authored
77 callback(:before_add, record)
85fbb22 @dhh Backed out of new_record? to new? transformation as it would screw up…
dhh authored
78 insert_record(record) unless @owner.new_record?
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
79 @target << record
0e92f36 @dhh Added callbacks on push_with_attributes #1594 [Florian Weber]
dhh authored
80 callback(:after_add, record)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
81
db045db @dhh Initial
dhh authored
82 self
83 end
e5684a2 @jamis update deprecations to include alternative methods (where available)
jamis authored
84 deprecate :push_with_attributes => "consider using has_many :through instead"
50f538b @jeremy Allow :uniq => true with has_many :through associations.
jeremy authored
85
db045db @dhh Initial
dhh authored
86 alias :concat_with_attributes :push_with_attributes
87
88 protected
a5a82d9 @dhh Added extension capabilities to has_many and has_and_belongs_to_many …
dhh authored
89 def method_missing(method, *args, &block)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
90 if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
a5a82d9 @dhh Added extension capabilities to has_many and has_and_belongs_to_many …
dhh authored
91 super
92 else
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
93 @reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
94 @reflection.klass.send(method, *args, &block)
a5a82d9 @dhh Added extension capabilities to has_many and has_and_belongs_to_many …
dhh authored
95 end
96 end
97 end
50f538b @jeremy Allow :uniq => true with has_many :through associations.
jeremy authored
98
db045db @dhh Initial
dhh authored
99 def count_records
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
100 load_target.size
db045db @dhh Initial
dhh authored
101 end
102
103 def insert_record(record)
85fbb22 @dhh Backed out of new_record? to new? transformation as it would screw up…
dhh authored
104 if record.new_record?
a2f26b9 @dhh Fixed that adding a record to a has_and_belongs_to collection would a…
dhh authored
105 return false unless record.save
106 end
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
107
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
108 if @reflection.options[:insert_sql]
109 @owner.connection.execute(interpolate_sql(@reflection.options[:insert_sql], record))
db045db @dhh Initial
dhh authored
110 else
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
111 columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns")
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
112
113 attributes = columns.inject({}) do |attributes, column|
114 case column.name
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
115 when @reflection.primary_key_name
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
116 attributes[column.name] = @owner.quoted_id
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
117 when @reflection.association_foreign_key
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
118 attributes[column.name] = record.quoted_id
119 else
83e2f6a @jamis Allow unspecified join-table columns to use to their default values w…
jamis authored
120 if record.attributes.has_key?(column.name)
85fbb22 @dhh Backed out of new_record? to new? transformation as it would screw up…
dhh authored
121 value = @owner.send(:quote_value, record[column.name], column)
83e2f6a @jamis Allow unspecified join-table columns to use to their default values w…
jamis authored
122 attributes[column.name] = value unless value.nil?
123 end
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
124 end
125 attributes
126 end
127
128 sql =
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
129 "INSERT INTO #{@reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " +
9870396 @dhh Fixed incompatibility in DB2 adapter with the new limit/offset approa…
dhh authored
130 "VALUES (#{attributes.values.join(', ')})"
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
131
db045db @dhh Initial
dhh authored
132 @owner.connection.execute(sql)
133 end
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored
134
135 return true
db045db @dhh Initial
dhh authored
136 end
ac3c8a5 @dhh Silence errors occurring when reloading classes
dhh authored
137
db045db @dhh Initial
dhh authored
138 def delete_records(records)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
139 if sql = @reflection.options[:delete_sql]
190e046 @dhh Fixed that :delete_sql in has_and_belongs_to_many associations couldn…
dhh authored
140 records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) }
db045db @dhh Initial
dhh authored
141 else
142 ids = quoted_record_ids(records)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
143 sql = "DELETE FROM #{@reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{@owner.quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})"
db045db @dhh Initial
dhh authored
144 @owner.connection.execute(sql)
145 end
146 end
a3502c4 @technoweenie Use association's :conditions when eager loading. [jeremyevans0@gmail…
technoweenie authored
147
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
148 def construct_sql
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
149 interpolate_sql_options!(@reflection.options, :finder_sql)
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored
150
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
151 if @reflection.options[:finder_sql]
152 @finder_sql = @reflection.options[:finder_sql]
71bdf13 @dhh Removed the default order by id on has_and_belongs_to_many queries as…
dhh authored
153 else
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored
154 @finder_sql = "#{@reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{@owner.quoted_id} "
a3502c4 @technoweenie Use association's :conditions when eager loading. [jeremyevans0@gmail…
technoweenie authored
155 @finder_sql << " AND (#{conditions})" if conditions
71bdf13 @dhh Removed the default order by id on has_and_belongs_to_many queries as…
dhh authored
156 end
d21ba5a @jeremy has_and_belongs_to_many: use JOIN instead of LEFT JOIN. References #…
jeremy authored
157
56af14d @technoweenie Changed has_and_belongs_to_many join to INNER JOIN for Mysql 3.23.x. …
technoweenie authored
158 @join_sql = "INNER JOIN #{@reflection.options[:join_table]} ON #{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{@reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
159 end
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored
160
161 # Join tables with additional columns on top of the two foreign keys must be considered ambigious unless a select
162 # clause has been explicitly defined. Otherwise you can get broken records back, if, say, the join column also has
163 # and id column, which will then overwrite the id column of the records coming back.
164 def finding_with_ambigious_select?(select_clause)
165 !select_clause && @owner.connection.columns(@reflection.options[:join_table], "Join Table Columns").size != 2
166 end
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored
167 end
db045db @dhh Initial
dhh authored
168 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming…
dhh authored
169 end
Something went wrong with that request. Please try again.