我正在编写一个范围,应该选择所有调用,其中call_status = open并且unit_id不为零。我对 Ruby 真的很弱,而且对 Rails 也很陌生,很难正确表达这一点。
这是我所拥有的:
scope :open_calls, where(:call_status => "open", :unit_id != nil).order("id ASC")
我应该使用不同的运算符来评估 nil 吗?
最佳答案
据我所知,没有办法告诉 ActiveRecord 使用哈希构建 NULL 子句。但是您可以链接字符串样式和散列样式的 where 子句:
scope :open_calls, where(:call_status => "open").where("unit_id IS NOT NULL").order("id ASC")
关于ruby-on-rails-3 - Rails 范围不等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11320945/