gpt4 book ai didi

ruby-on-rails - rails 2.3 : How to turn this SQL statement into a named_scope

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

弄清楚如何从这个 SQL 查询创建一个 named_scope 有点困难:

select * from foo where id NOT IN (select foo_id from bar) AND foo.category = ?按 RAND() 限制 1 排序;

类别应该是可变的。

针对上述问题编写命名范围的最有效方式是什么?

最佳答案

  named_scope :scope_name, lambda { |category|
{
:conditions => ["id NOT IN (select foo_id from bar) AND foo.category = ?", category],
:order => 'RAND()',
:limit => 1
}
}

关于ruby-on-rails - rails 2.3 : How to turn this SQL statement into a named_scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2937537/

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