Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escaping column names #8

Open
Tragos opened this issue Jul 12, 2010 · 2 comments
Open

Escaping column names #8

Tragos opened this issue Jul 12, 2010 · 2 comments

Comments

@Tragos
Copy link

Tragos commented Jul 12, 2010

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.

@lenary
Copy link

lenary commented Jul 12, 2010

why not just use a ? and put the object_class.connection.call(:method) in the array...

@Tragos
Copy link
Author

Tragos commented Jul 12, 2010

Using ? would transform it into a string, like: 'column_name' (see the difference between grave accents and single quotes), to which MySQL would treat as a literal value and not a column, as opposed to column_name.
(edit: this comment system parses grave accents into code, but I hope you get the idea.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants