Class: ActiveRecord::Associations::HasManyAssociation

Inherits:
AssociationCollection
  • Object
show all
Defined in:
lib/composite_primary_keys/associations.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#construct_sqlObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/composite_primary_keys/associations.rb', line 225

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 @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]
    # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */
    @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