2014-06-18 20 views
0

我有一個應用程序使用Ruby on Rails 3.1.11,ruby 1.9.2,mysql 5.6.19。我的模型之一叫做「ManagedFile」,它包含一個名爲「group」的列無法在Rails 3.1.11中的活動記錄中的「group」上查詢

我能夠查詢

ManagedFile.select :name 
ManagedFile.select "name" 
ManagedFile.select :id 
ManagedFile.select :exported, etc 
ManagedFile.select('distinct name') 
ManagedFile.select('distinct id'), etc 

but not ManagedFile.select :group 
but not ManagedFile.select "group" 
nor ManagedFile.select('distinct group') 

它只會引發異常

1.9.2-head :067 > ManagedFile.select('distinct group') 
      ManagedFile Load (0.2ms) SELECT distinct group FROM `managed_files` 
      Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group FROM `managed_files`' at line 1: SELECT distinct group FROM `managed_files` 
      ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group FROM `managed_files`' at line 1: SELECT distinct group FROM `managed_files` 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:906:in `prepare' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:906:in `exec_stmt' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:443:in `block in exec_query' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activesupport-3.1.11/lib/active_support/notifications/instrumenter.rb:21:in `instrument' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:442:in `exec_query' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/mysql_adapter.rb:976:in `select' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract/database_statements.rb:20:in `select_all' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/base.rb:470:in `find_by_sql' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/relation.rb:112:in `to_a' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/activerecord-3.1.11/lib/active_record/relation.rb:437:in `inspect' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/railties-3.1.11/lib/rails/commands/console.rb:45:in `start' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/railties-3.1.11/lib/rails/commands/console.rb:8:in `start' 
     from /secret_path/.rvm/gems/[email protected]_1_11/gems/railties-3.1.11/lib/rails/commands.rb:40:in `<top (required)>' 
     from script/rails:6:in `require' 
     from script/rails:6:in `<main>'1.9.2-head :068 > 
+0

的可能重複的[在列名保留字 - 插入到MySQL](http://stackoverflow.com/questions/9800075/reserved-word-in-column-name-insert-into-mysql) –

回答

1

關鍵字group在MySQL中保留。重命名列(推薦),或者使用反引號(`)來轉義它。

ManagedFile.select("`group`")