4

I have a employee data base that captures all employees for all companies the companies are referenced by a 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 have been reading the Ruby on Rails Guides and cant seem to find what i am looking for. sorry i have never done such a query before its probably formatted wrong or something

1
  • 1
    replace and with , so it looks like employee_ids = Employees.where(company_id: company_id, race: 'African', foreign: 1).pluck(:id_number) Commented Mar 13, 2013 at 7:48

1 Answer 1

8

You have to replace 'and' with ',' Try as follow;

employee_ids = Employees.where(company_id: company_id, race: 'African',  foreign: 1).pluck(:id_number)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.