gpt4 book ai didi

ruby-on-rails - rails : refactoring chained where clauses

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:26 26 4
gpt4 key购买 nike

重写这样的东西的最佳做法是什么?我觉得太麻烦了:

def filtered_components(template)
components.joins(:templates).where(templates: { id: template.id }).where(state: 'working').where(['fake_created_at < ?', Time.now]).order('created_at DESC')
end

一定有更好的方法,对吧?

谢谢。

最佳答案

我只会有一个 where 子句

def filtered_components(template)
components.joins(:templates).where("templates.id = ? AND components.state = ? AND components.fake_created_at < ?", template.id, 'working', Time.now).order('created_at DESC')
end

对我来说看起来很清楚。

关于ruby-on-rails - rails : refactoring chained where clauses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842597/

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