ActiveRecord query using multiple conditions

I have an employee database that captures all employees for all the companies the companies refer to, company_id

I want to do something like this

 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 } 

Like this

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

I keep getting the following error:

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

And they read Ruby on Rails Guides and could not find what I was looking for. it’s a pity that I never made such a request before it was probably formatted incorrectly or something

+4
source share
1 answer

You should replace 'and' with ',' Try the following:

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

Source: https://habr.com/ru/post/1468904/


All Articles