124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/composite_primary_keys/associations.rb', line 124
def composite_association_join
join = case reflection.macro
when :has_and_belongs_to_many
" LEFT OUTER JOIN %s ON %s " % [
table_alias_for(options[:join_table], aliased_join_table_name),
composite_join_clause(
full_keys(aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key),
full_keys(reflection.active_record.table_name, reflection.active_record.primary_key)
)
] +
" LEFT OUTER JOIN %s ON %s " % [
table_name_and_alias,
composite_join_clause(
full_keys(aliased_table_name, klass.primary_key),
full_keys(aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key)
)
]
when :has_many, :has_one
case
when reflection.macro == :has_many && reflection.options[:through]
through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
if through_reflection.options[:as] raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
else
if source_reflection.macro == :has_many && source_reflection.options[:as]
raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
else
case source_reflection.macro
when :belongs_to
first_key = primary_key
second_key = options[:foreign_key] || klass.to_s.classify.foreign_key
when :has_many
first_key = through_reflection.klass.to_s.classify.foreign_key
second_key = options[:foreign_key] || primary_key
end
" LEFT OUTER JOIN %s ON %s " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
composite_join_clause(
full_keys(aliased_join_table_name, through_reflection.primary_key_name),
full_keys(parent.aliased_table_name, parent.primary_key)
)
] +
" LEFT OUTER JOIN %s ON %s " % [
table_name_and_alias,
composite_join_clause(
full_keys(aliased_table_name, first_key),
full_keys(aliased_join_table_name, second_key)
)
]
end
end
when reflection.macro == :has_many && reflection.options[:as]
raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
when reflection.macro == :has_one && reflection.options[:as]
raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
else
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
" LEFT OUTER JOIN %s ON %s " % [
table_name_and_alias,
composite_join_clause(
full_keys(aliased_table_name, foreign_key),
full_keys(parent.aliased_table_name, parent.primary_key)),
]
end
when :belongs_to
" LEFT OUTER JOIN %s ON %s " % [
table_name_and_alias,
composite_join_clause(
full_keys(aliased_table_name, reflection.klass.primary_key),
full_keys(parent.aliased_table_name, options[:foreign_key] || klass.to_s.foreign_key)),
]
else
""
end || ''
join << %(AND %s.%s = %s ) % [
aliased_table_name,
reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column),
klass.connection.quote(klass.name)] unless klass.descends_from_active_record?
join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
join
end
|