4

我有一个员工数据库,该数据库捕获了所有公司引用的所有公司的所有员工company_id

我想做这样的事情

sql = "SELECT race, `foreign`, id_number, company_id, COUNT(*) FROM `employees` WHERE company_id = 52 AND race = `African` AND `foreign` = 1 GROUP BY id_number;"
temp_arr = []
ActiveRecord::Base.connection.execute(sql).each {|int| temp_arr << int }

像这样

employee_ids = Employees.where(company_id: company_id and race: 'African' and foreign: 1).pluck(:id_number)

我不断收到以下错误

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'African' in 'where clause':

并且一直在阅读Ruby on Rails 指南,但似乎找不到我要找的东西。抱歉,在可能格式错误或其他问题之前,我从未做过这样的查询

4

1 回答 1

8

您必须将 'and' 替换为 ',' 尝试如下;

employee_ids = Employees.where(company_id: company_id, race: 'African',  foreign: 1).pluck(:id_number)
于 2013-03-13T07:49:05.530 回答