Class: ActiveRecord::Associations::HasManyAssociation
- Inherits:
-
AssociationCollection
- Object
- AssociationCollection
- ActiveRecord::Associations::HasManyAssociation
show all
- Defined in:
- lib/composite_primary_keys/associations.rb
Overview
Instance Method Summary
collapse
Instance Method Details
#construct_sql ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/composite_primary_keys/associations.rb', line 313
def construct_sql
case
when @reflection.options[:finder_sql]
@finder_sql = interpolate_sql(@reflection.options[:finder_sql])
when @reflection.options[:as]
@finder_sql =
"#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
"#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
@finder_sql << " AND (#{conditions})" if conditions
else
@finder_sql = full_columns_equals(@reflection.klass.table_name, @reflection.primary_key_name, @owner.quoted_id)
@finder_sql << " AND (#{conditions})" if conditions
end
if @reflection.options[:counter_sql]
@counter_sql = interpolate_sql(@reflection.options[:counter_sql])
elsif @reflection.options[:finder_sql]
@reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" }
@counter_sql = interpolate_sql(@reflection.options[:counter_sql])
else
@counter_sql = @finder_sql
end
end
|
#delete_records(records) ⇒ Object
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/composite_primary_keys/associations.rb', line 340
def delete_records(records)
if @reflection.options[:dependent]
records.each { |r| r.destroy }
else
field_names = @reflection.primary_key_name.split(',')
field_names.collect! {|n| n + " = NULL"}
records.each do |r|
where_class = nil
if r.quoted_id.include?(',')
where_class = [@reflection.klass.primary_key, r.quoted_id].transpose.map {|pair| "(#{pair[0]} = #{pair[1]})"}.join(" AND ")
else
where_class = @reflection.klass.primary_key + ' = ' + r.quoted_id
end
@reflection.klass.update_all( field_names.join(',') , where_class)
end
end
end
|