Ruby 在 Rails Active Record - Where 条件和这个条件或那个条件
Ruby On Rails Active Record - Where Condition AND This Condition or That Condition
我有一个简单的时间跟踪器,只是试图编写一个满足以下条件的查询,并使用以下内容提取所有记录:user_id = 6 AND (is_paused 为真或 manual_input_hours 为零)。
基于我尝试构建的内容如下
Timerecord.where(user_id: 6).where(Timerecord.where(is_paused: true).or(Timerecord.where.not(manual_input_hours: 0)))
但它不起作用,出现以下错误:不支持的参数类型:#Timerecord::ActiveRecord_Relation:0x000056546f549358 (Timerecord::ActiveRecord_Relation) (ArgumentError)
您的 OR 部分正确,只需将其与用户查询结合起来即可。您可以使用合并方法执行此操作以获得 AND 行为:
records_for_user = Timerecord.where(user_id: 6)
paused = Timerecord.where(is_paused: true)
has_manual_input = Timerecord.where.not(manual_input_hours: 0)
# records_for_user AND (paused OR has_manual_input)
records_for_user.merge(paused.or(has_manual_input))
我有一个简单的时间跟踪器,只是试图编写一个满足以下条件的查询,并使用以下内容提取所有记录:user_id = 6 AND (is_paused 为真或 manual_input_hours 为零)。
基于我尝试构建的内容如下
Timerecord.where(user_id: 6).where(Timerecord.where(is_paused: true).or(Timerecord.where.not(manual_input_hours: 0)))
但它不起作用,出现以下错误:不支持的参数类型:#Timerecord::ActiveRecord_Relation:0x000056546f549358 (Timerecord::ActiveRecord_Relation) (ArgumentError)
您的 OR 部分正确,只需将其与用户查询结合起来即可。您可以使用合并方法执行此操作以获得 AND 行为:
records_for_user = Timerecord.where(user_id: 6)
paused = Timerecord.where(is_paused: true)
has_manual_input = Timerecord.where.not(manual_input_hours: 0)
# records_for_user AND (paused OR has_manual_input)
records_for_user.merge(paused.or(has_manual_input))