Skip to content
Newer
Older
100644 170 lines (139 sloc) 6.06 KB
db045db @dhh Initial
dhh authored Nov 24, 2004
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 Dec 3, 2005
4 def initialize(owner, reflection)
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
5 super
6 construct_sql
db045db @dhh Initial
dhh authored Nov 24, 2004
7 end
8
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
9 def build(attributes = {})
10 load_target
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
11 record = @reflection.klass.new(attributes)
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
12 @target << record
13 record
14 end
15
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
16 def find_first
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
17 load_target.first
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
18 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
19
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
20 def find(*args)
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
21 options = Base.send(:extract_options_from_args!, args)
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
22
23 # If using a custom finder_sql, scan the entire collection.
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
24 if @reflection.options[:finder_sql]
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
25 expects_array = args.first.kind_of?(Array)
26 ids = args.flatten.compact.uniq
27
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
28 if ids.size == 1
4307d7e @dhh Fixed various problems with has_and_belongs_to_many when using custom…
dhh authored Sep 13, 2005
29 id = ids.first.to_i
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
30 record = load_target.detect { |record| id == record.id }
4307d7e @dhh Fixed various problems with has_and_belongs_to_many when using custom…
dhh authored Sep 13, 2005
31 expects_array ? [record] : record
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
32 else
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
33 load_target.select { |record| ids.include?(record.id) }
6bd672e @dhh Added that Base#find takes an optional options hash, including :condi…
dhh authored Jan 1, 2005
34 end
db045db @dhh Initial
dhh authored Nov 24, 2004
35 else
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
36 conditions = "#{@finder_sql}"
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
37
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
38 if sanitized_conditions = sanitize_sql(options[:conditions])
328ef3f @jeremy Parenthesize :conditions
jeremy authored Oct 18, 2005
39 conditions << " AND (#{sanitized_conditions})"
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
40 end
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
41
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
42 options[:conditions] = conditions
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored Apr 1, 2006
43 options[:joins] = @join_sql
44 options[:readonly] = finding_with_ambigious_select?(options[:select])
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
45
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
46 if options[:order] && @reflection.options[:order]
47 options[:order] = "#{options[:order]}, #{@reflection.options[:order]}"
48 elsif @reflection.options[:order]
49 options[:order] = @reflection.options[:order]
db045db @dhh Initial
dhh authored Nov 24, 2004
50 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
51
e5d9ad3 @dhh Added option inheritance for find calls on has_and_belongs_to_many an…
dhh authored Dec 13, 2005
52 merge_options_from_reflection!(options)
53
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
54 # Pass through args exactly as we received them.
55 args << options
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
56 @reflection.klass.find(*args)
db045db @dhh Initial
dhh authored Nov 24, 2004
57 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
58 end
db045db @dhh Initial
dhh authored Nov 24, 2004
59
60 def push_with_attributes(record, join_attributes = {})
61 raise_on_type_mismatch(record)
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
62 join_attributes.each { |key, value| record[key.to_s] = value }
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
63
0e92f36 @dhh Added callbacks on push_with_attributes #1594 [Florian Weber]
dhh authored Jul 5, 2005
64 callback(:before_add, record)
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
65 insert_record(record) unless @owner.new_record?
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
66 @target << record
0e92f36 @dhh Added callbacks on push_with_attributes #1594 [Florian Weber]
dhh authored Jul 5, 2005
67 callback(:after_add, record)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
68
db045db @dhh Initial
dhh authored Nov 24, 2004
69 self
70 end
71
72 alias :concat_with_attributes :push_with_attributes
73
74 def size
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
75 @reflection.options[:uniq] ? count_records : super
db045db @dhh Initial
dhh authored Nov 24, 2004
76 end
77
78 protected
a5a82d9 @dhh Added extension capabilities to has_many and has_and_belongs_to_many …
dhh authored Nov 4, 2005
79 def method_missing(method, *args, &block)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
80 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 Nov 4, 2005
81 super
82 else
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
83 @reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
84 @reflection.klass.send(method, *args, &block)
a5a82d9 @dhh Added extension capabilities to has_many and has_and_belongs_to_many …
dhh authored Nov 4, 2005
85 end
86 end
87 end
88
4307d7e @dhh Fixed various problems with has_and_belongs_to_many when using custom…
dhh authored Sep 13, 2005
89 def find_target
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
90 if @reflection.options[:finder_sql]
91 records = @reflection.klass.find_by_sql(@finder_sql)
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
92 else
e5d9ad3 @dhh Added option inheritance for find calls on has_and_belongs_to_many an…
dhh authored Dec 13, 2005
93 records = find(:all)
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
94 end
95
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
96 @reflection.options[:uniq] ? uniq(records) : records
db045db @dhh Initial
dhh authored Nov 24, 2004
97 end
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
98
db045db @dhh Initial
dhh authored Nov 24, 2004
99 def count_records
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
100 load_target.size
db045db @dhh Initial
dhh authored Nov 24, 2004
101 end
102
103 def insert_record(record)
a2f26b9 @dhh Fixed that adding a record to a has_and_belongs_to collection would a…
dhh authored Jun 18, 2005
104 if record.new_record?
105 return false unless record.save
106 end
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
107
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
108 if @reflection.options[:insert_sql]
109 @owner.connection.execute(interpolate_sql(@reflection.options[:insert_sql], record))
db045db @dhh Initial
dhh authored Nov 24, 2004
110 else
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
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 Jan 24, 2005
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 Dec 3, 2005
115 when @reflection.primary_key_name
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
116 attributes[column.name] = @owner.quoted_id
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
117 when @reflection.association_foreign_key
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
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 Jul 18, 2005
120 if record.attributes.has_key?(column.name)
121 value = @owner.send(:quote, record[column.name], column)
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 Jan 24, 2005
124 end
125 attributes
126 end
127
128 sql =
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
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 approach
dhh authored Jul 17, 2005
130 "VALUES (#{attributes.values.join(', ')})"
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
131
db045db @dhh Initial
dhh authored Nov 24, 2004
132 @owner.connection.execute(sql)
133 end
b29c01e @dhh Added that has_and_belongs_to_many associations with additional attri…
dhh authored Jan 24, 2005
134
135 return true
db045db @dhh Initial
dhh authored Nov 24, 2004
136 end
ac3c8a5 @dhh Silence errors occurring when reloading classes
dhh authored Nov 30, 2004
137
db045db @dhh Initial
dhh authored Nov 24, 2004
138 def delete_records(records)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
139 if sql = @reflection.options[:delete_sql]
190e046 @dhh Fixed that :delete_sql in has_and_belongs_to_many associations couldn…
dhh authored May 19, 2005
140 records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) }
db045db @dhh Initial
dhh authored Nov 24, 2004
141 else
142 ids = quoted_record_ids(records)
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
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 Nov 24, 2004
144 @owner.connection.execute(sql)
145 end
146 end
a3502c4 @technoweenie Use association's :conditions when eager loading. [jeremyevans0@gmail…
technoweenie authored Mar 16, 2006
147
823554e @dhh Added support for associating unsaved objects #402 [Tim Bates]
dhh authored Jan 15, 2005
148 def construct_sql
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
149 interpolate_sql_options!(@reflection.options, :finder_sql)
34f9d30 @dhh Added support for calling constrained class methods on has_many and h…
dhh authored Jul 22, 2005
150
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
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 Apr 13, 2005
153 else
6abda69 @dhh Added preliminary support for join models [DHH] Added preliminary sup…
dhh authored Dec 3, 2005
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 Mar 16, 2006
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 Apr 13, 2005
156 end
d21ba5a @jeremy has_and_belongs_to_many: use JOIN instead of LEFT JOIN. References #2937
jeremy authored Nov 21, 2005
157
56af14d @technoweenie Changed has_and_belongs_to_many join to INNER JOIN for Mysql 3.23.x. …
technoweenie authored Mar 26, 2006
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 Jan 15, 2005
159 end
53aa8da @dhh Fixed that records returned from has_and_belongs_to_many associations…
dhh authored Apr 1, 2006
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 Jan 15, 2005
167 end
db045db @dhh Initial
dhh authored Nov 24, 2004
168 end
68d1056 @dhh Fixed that has_and_belongs_to_many would generate bad sql when naming…
dhh authored Dec 14, 2004
169 end
Something went wrong with that request. Please try again.