开发者

Rails 3: scope is not true

开发者 https://www.devze.com 2023-04-12 22:09 出处:网络
How do I convert the following into something that works on sqlite and mysql? Note, internal is nullable.

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)


ActiveRecord is smart enough for doing this, just do:

scope :external, where(internal: [false, nil])

That would be transformed in similar SQL (with table name and quetes probably):

  • for PostgreSQL and SQLite internal IN ('f') OR internal IS NULL
  • for MySQL internal IN (0) OR internal IS NULL

If need more control you can use Arel:

scope :external,
  where(arel_table[:internal].eq(false).or(arel_table[:internal].eq(nil)))

Which would be:

  • for PostgreSQL and SQLite internal = 'f' OR internal IS NULL
  • for MySQL internal = 0 OR internal IS NULL

Remember, if you need to check NULL values in SQL always use IS NULL or IS NOT NULL expressions.

Expresions ... <> NULL, ... = NULL, ... IN (NULL) are evaluated as false for everything.

So, NULL <> NULL is false and NUL = NULL is false.


Try scope :external, where('internal IN (?)', [false,nil])


Try scope :external, where(:internal => !true)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号