Rails 3:范围不正确
如何将以下内容转换为适用于 sqlite 和 mysql 的内容?请注意,internal
可为空。
scope :external, where("internal is not true")
(在 Rails 4 中,可以简单地使用:)
scope :external, where.not(internal: true)
How do I convert the following into something that works on sqlite and mysql? Note, internal
is nullable.
scope :external, where("internal is not true")
(In Rails 4, one can simply use:)
scope :external, where.not(internal: true)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ActiveRecord 足够聪明,可以做到这一点,只需执行以下操作:
这将用类似的 SQL 进行转换(可能带有表名和队列):
internal IN ('f') OR internal IS NULL
内部 IN (0) OR 内部 IS NULL
如果需要更多控制,您可以使用 Arel:
对于
internal = 'f' OR 内部 IS NULL
internal = 0 OR internal IS NULL
请记住,如果您需要在 SQL 中检查 NULL 值,请始终使用
IS NULL
或IS NOT NULL
表达式。表达式
... <> NULL
、... = NULL
、... IN (NULL)
对于所有内容都被评估为 false。所以,
NULL <> NULL
为假,NUL = NULL
为假。ActiveRecord is smart enough for doing this, just do:
That would be transformed in similar SQL (with table name and quetes probably):
internal IN ('f') OR internal IS NULL
internal IN (0) OR internal IS NULL
If need more control you can use Arel:
Which would be:
internal = 'f' OR internal IS NULL
internal = 0 OR internal IS NULL
Remember, if you need to check NULL values in SQL always use
IS NULL
orIS NOT NULL
expressions.Expresions
... <> NULL
,... = NULL
,... IN (NULL)
are evaluated as false for everything.So,
NULL <> NULL
is false andNUL = NULL
is false.尝试作用域:external, where('internal IN (?)', [false,nil])
Try scope :external, where('internal IN (?)', [false,nil])
尝试
scope :external, where(:internal => !true)
Try
scope :external, where(:internal => !true)