Rails Mysql2 ::错误:操作数应包含1列

时间:2014-06-10 08:48:06

标签: sql ruby-on-rails

我想采用这种方法:

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

现在想把它分成不同的性别。我们在参与者表中捕获性别。所以我尝试在查询中使用活动记录array conditions来这样做:

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')

但我不断收到以下错误:

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'):

我已经削减了我的查询,我似乎无法找到我做错了什么?

1 个答案:

答案 0 :(得分:5)

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

应该是

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

要获得SQL结果,请将构建查询的方式更改为:

participants.race IN (?)