【发布时间】:2020-01-01 06:42:07
【问题描述】:
我的 rails 应用程序中有一个用户表,该应用程序在整个应用程序的许多控制器方法中为此模型使用了许多 where 条件。
现在我必须为 where 条件添加一个额外的属性。
有没有办法做到以下几点以及如何做?我可以将自定义where 写入用户模型,而不是将额外属性添加到整个应用程序中使用的所有 where 条件,因此该条件将被预先添加到用户模型的整个应用程序中的where。
我找到了哪里的来源
def where(opts = :chain, *rest)
if :chain == opts
WhereChain.new(spawn)
elsif opts.blank?
self
else
spawn.where!(opts, *rest)
end
end
我现在在控制器方法中的 where 条件:
User.where(:status => true, :country => "IN")
此条件和类似条件在应用程序中的许多方法中使用,我想获得没有:deactivated的用户。
我可以对所有 where 条件进行更改,例如
User.where(:status => true, :country => "IN", :deactivated => false)
相反,我想写一个自定义,其中预检查:deactivated => false
【问题讨论】:
标签: ruby-on-rails activerecord