使用多个条件的ActiveRecord查询

时间:2013-03-13 07:45:57

标签: ruby-on-rails activerecord

我有一个员工数据库,可以抓住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 Guides,似乎无法找到我要找的东西。对不起,我之前从来没有做过这样的查询,可能是格式错误或其他什么

1 个答案:

答案 0 :(得分:8)

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

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