ernie/squeel — Issue #84: MySQL error when trying to use a Rails finder with a symbol
Author: dvandersluis
Created: 2011-12-06 22:16:16+00:00
State: open

Expected behavior:

> Language.find_by_locale :en
Language Load (0.6ms) SELECT `languages`.* FROM `languages` WHERE `languages`.`locale` = 'en' LIMIT 1
=> #

Actual behaviour with squeel 0.9.3 enabled in my Gemfile:

> Language.find_by_locale :en
Language Load (0.8ms) SELECT `languages`.* FROM `languages` WHERE `languages`.`locale` = `languages`.`en` LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'languages.en' in 'where clause': SELECT `languages`.* FROM `languages` WHERE `languages`.`locale` = `languages`.`en` LIMIT 1
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_query'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:685:in `exec_query'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:679:in `select'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:470:in `find_by_sql'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/relation.rb:111:in `to_a'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/relation/finder_methods.rb:376:in `find_first'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/relation/finder_methods.rb:122:in `first'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/relation/finder_methods.rb:263:in `find_by_attributes'
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:1063:in `method_missing'
from (irb):1

Rails reverts to the expected behaviour if I comment out the `gem 'squeel'` line from my `Gemfile`. The same behaviour occurs with `Language.where(:locale => :en)` (works without squeel; `Mysql2::Error` with squeel). Using a string instead of a symbol works in all cases.