我该如何正确编写?
Request.pending.where(.....) ## Request.pending = request.state_id in (1..3)
这些条件是:
approver1_id = current_user and state_id = 1
or
approver2_id = current_user and state_id = 2
or
approver3_id = current_user and state_id = 3
如果我也可以将这些条件放在模型中以供其他控制器/视图使用,那将是非常好的,因为我将在整个应用程序中经常使用这些条件。
尝试:
Request.pending.where(
'(approver1_id= ? AND state_id= ?) OR
(approver2_id= ? AND state_id= ?) OR
(approver3_id= ? AND state_id= ?)',
current_user.id,
1,
current_user.id,
2,
current_user.id,
3
)
编辑:我忘了你应该使用冒号。而且不应该是“ current_user.id”吗?还不清楚您的请求是使用三个参数approver1_id-approver3_id还是每个请求仅使用一个approver_id。
编辑2:将查询更改为SQL。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句