wvanbergen / active_olap

OLAP extensions for ActiveRecord
http://wiki.github.com/wvanbergen/active_olap
MIT License
55 stars 7 forks source link

Not handling ambiguous column names correctly with :joins #2

Closed sadowski closed 14 years ago

sadowski commented 14 years ago

If you run an olap query with a :joins, the 'id' column will be considered ambiguous. You need to prepend the table name to the front of the column for each dimension. In dimension.rb, line 58, add the @klass.connection.send(:quote_table_name, @klass.table_name) + '.' + to the beginning of the column name:

def to_case_expression(variable_name)
  if @category_field
    @klass.connection.send(:quote_table_name, @klass.table_name) + '.' + 
      "#{@klass.connection.send(:quote_column_name, @category_field)} AS #{@klass.connection.send(:quote_column_name, variable_name)}"
  else
    whens = @categories.map { |category| @klass.send(:sanitize_sql, ["WHEN (#{category.to_sanitized_sql}) THEN ?", category.label.to_s]) }
    "CASE #{whens.join(' ')} ELSE NULL END AS #{@klass.connection.send(:quote_column_name, variable_name)}";
  end
end

You'll probably want to implement a more robust solution though.

wvanbergen commented 14 years ago

Sorry for the late reply, but I finally fixed this issue. It is included in release version 0.0.5.