关于范围的 Ruby on Rails 问题
我的模型中有以下命名范围
class User
scope :single_action, where("parent_id = ?", nil)
end
即使有parent_id 值为 nil 的记录,它们似乎也不可用。
ruby-1.9.2-p0 > User.select("name")
=> [#<User parent_id: nil, name: "John">, #<Task parent_id: 1, name: "Felix">, #<Task parent_id: nil, name: "John Felix">]
我尝试使用活动记录查询来执行相同的操作,但它也返回空结果集
ruby-1.9.2-p0 > User.where("parent_id = ?",nil)
=> []
我定义了一些其他范围,这些范围似乎工作正常。 我的Rails版本是3.0.7,Ruby版本是Ruby 1.9.2 你能帮我解决这个问题吗?
谢谢 :)
I have the following named scope in my model
class User
scope :single_action, where("parent_id = ?", nil)
end
Even though there are records with parent_id with nil values, they do not seem to be available.
ruby-1.9.2-p0 > User.select("name")
=> [#<User parent_id: nil, name: "John">, #<Task parent_id: 1, name: "Felix">, #<Task parent_id: nil, name: "John Felix">]
I tried using the active record query to do the same, but it also returns empty resultset
ruby-1.9.2-p0 > User.where("parent_id = ?",nil)
=> []
I have defined few other scopes which seems to be working fine.
My Rails version is 3.0.7 and Ruby version is Ruby 1.9.2
Could you please help me to solve this problem.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改为:
您可以看到差异(您实际上是在尝试匹配字符串“NULL”,而不是检查值是否为 NULL):
Change that to :
You can see the difference (you were actually trying to match the string 'NULL' instead of checking for the value being NULL):