Reputation: 13002
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?
Upvotes: 0
Views: 2115
Reputation: 15515
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 (?)
Upvotes: 6