Skip to content

Escaping column names #8

@Tragos

Description

@Tragos

I've ran into an issue where my legacy tables use mysql reserved words (in my case, "to") as column names, and the autocomplete plugin fails to fetch the data due to a MySQL error.

To fix this, I added "quote_column_name()" calls around the "method" variables in the "auto_complete_for" method:

def auto_complete_for(object, method, options = {})
  define_method("auto_complete_for_#{object}_#{method}") do
    object_class = object.to_s.camelize.constantize
    find_options = { 
      :conditions => [ "LOWER(#{object_class.connection.quote_column_name(method)}) LIKE ?", '%' + params[object][method].downcase + '%' ],
      :order => "#{object_class.connection.quote_column_name(method)} ASC",
      :limit => 10 }.merge!(options)

    @items = object_class.find(:all, find_options)

    render :inline => "<%= auto_complete_result @items, '#{method}' %>"
  end
end

I'm not sure this is an elegant solution though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions