0

I wanted to take this method:

self.spend_on_scholarships_for_african_people = training_programs.scholarships.joins(:participants).where('participants.race' => AFRICAN_RACES).sum('participants.spend')

and now want break it into different genders. We capture gender within the participants table. so i tried using active records array conditions within the query to do so like this:

self.bursary_spend_on_potential_female_employees = training_programs.scholarships.joins(:participants).where('participants.race = ? AND participants.gender = ?', AFRICAN_RACES, 'Female').sum('participants.spend')
self.bursary_spend_on_potential_male_employees = training_programs.scholarships.joins(:participants).where('participants.race = ? AND participants.gender = ?', AFRICAN_RACES, 'Male').sum('participants.spend')

But i keep getting the following error:

ActiveRecord::StatementInvalid - Mysql2::Error: Operand should contain 1 column(s): SELECT SUM(participants.spend) AS sum_id FROM `training_programs` INNER JOIN `participants` ON `participants`.`training_program_id` = `training_programs`.`id` WHERE `training_programs`.`skills_development_id` IS NULL AND `training_programs`.`skills_development_type` = 'SkillsDevelopment' AND `training_programs`.`scholarship` = 1 AND (participants.race = 'African','Coloured','Indian' AND participants.gender = 'Female'):

I have cut up my query and i can't seem to find what i have done wrong?

1 Answer 1

6
participants.race = 'African','Coloured','Indian'

should be

participants.race IN ('African','Coloured','Indian')

To get that SQL result, change the way you build your query to something like:

participants.race IN (?)
Sign up to request clarification or add additional context in comments.

1 Comment

zwippie you legend you!! it wasnt looking at it like an array. i was confused since it was working fine in the first instance. thank you so much!

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.