gpt4 book ai didi

ruby - Rails 3.2 CRUD : . 其中 'or' 有条件

转载 作者:数据小太阳 更新时间:2023-10-29 07:05:45 24 4
gpt4 key购买 nike

使用 ruby​​ on rails,我想做类似的事情:

 @tasks = Task.where(:def => true || :house_id => current_user.house_id)

执行此操作的最有效/最干净的方法是什么?

最佳答案

你可以这样做:

Task.where("def = ? or house_id = ?", true, current_user.house_id)

一般情况是:

Model.where("column = ? or other_column = ?", value, other_value)

您还可以利用 Arel:

t = Task.arel_table

@tasks = Task.where(
t[:def].eq(true).
or(t[:house_id].eq(current_user.house_id))
)

关于ruby - Rails 3.2 CRUD : . 其中 'or' 有条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12874577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com