ruby - 方法作为 ActiveRecord `where` 中每个实例的参数

标签 ruby activerecord

active record 中的.where 可以将方法作为参数吗?

def not_foo?
  name != "foo"
end

下面的代码不起作用,但你会明白我的意思:

array.where(|element| element.not_foo?).count

最佳答案

Can .where in active record take a method as an argument?

并且:不,它不能。也许你想这样做

array.where.not(name: 'foo').count

关于ruby - 方法作为 ActiveRecord `where` 中每个实例的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761226/

相关文章:

ruby - 如何为 Gemfile 指定最低 bundler 版本?

ruby - 数组与 Ruby 中重复元素的交集

Ruby:检查东亚宽度 (Unicode)

php - 如何使用 watir-webdriver 遍历 DOM(子级/兄弟级)?

ruby-on-rails - ActiveRecord 关联选择包含记录的计数

php - 查找可能的 URL 参数

ruby - 如何获取 ActiveRecord 3.x 中的所有范围列表

ruby-on-rails - 唯一性区分大小写 false 导致查询慢

ruby-on-rails - 如何在Rails的表格中计算唯一值?

ruby-on-rails - 具有许多列的 ActiveRecord 表 : is there a better way?