Rails6 のちょい足しな新機能を試す94（ActiveModel falsy symbol編）

# はじめに

Rails 6 に追加された新機能を試す第94段。 今回は、 `ActiveModel falsy simbol` 編です。
Rails 6 では、 `:false` など、`false` を連想させる symbol が ActiveModel (ActiveRecord) の値として、 false として扱われるようになりました。

Ruby 2.6.4, Rails 6.0.0 で確認しました。

なお、こちらは、Rails 5.2.4 以降でも同様の振舞いに変更されるものと思われます。

```shell
$ rails --version
Rails 6.0.0
```

# プロジェクトを作る

```shell
$ rails new rails_sandbox
$ cd rails_sandbox
```

今回は Book モデルを作って rails console で確認してみます。


# Book モデルを作る

`title` と `published` の2つの属性を持つ Book モデルを作ります。

```shell
$ bin/rails g model Book title published:boolean
```

# seed データを作る

seed データを作ります。

```ruby:db/seeds.rb
Book.create(
  [
    { title: 'Agile Web Development with Rails 5.1', published: true },
    { title: 'Agile Web Development with Rails 6', published: false }
  ]
)
```

# rails console を実行する

rails console を使って確認してみます。

`:"0"`, `:f`, `:F`, `:false`, `:FALSE`, `:off`, `:OFF` が falsy な値として扱われます。

`ActiveModel::Type::Boolean::FALSE_VALUES` でどの値が falsy な値となるのか確認することができます。

```irb
irb(main):001:0> ActiveModel::Type::Boolean::FALSE_VALUES
=> #<Set: {false, 0, "0", :"0", "f", :f, "F", :F, "false", :false, "FALSE", :FALSE, "off", :off, "OFF", :OFF}>
```

:"0" の場合

```irb
irb(main):002:0> Book.where(published: :"0")
  Book Load (0.3ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

:f の場合

```irb
irb(main):003:0> Book.where(published: :f)
  Book Load (0.8ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

:F の場合

```irb
irb(main):004:0> Book.where(published: :F)
  Book Load (0.8ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

:false の場合

```irb
irb(main):005:0> Book.where(published: :false)
  Book Load (0.7ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

:FALSE の場合

```irb
irb(main):006:0> Book.where(published: :FALSE)
  Book Load (0.7ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

:off の場合

```irb
irb(main):007:0> Book.where(published: :off)
  Book Load (0.8ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

:OFF の場合

```irb
irb(main):008:0> Book.where(published: :OFF)
  Book Load (0.7ms)  SELECT "books".* FROM "books" WHERE "books"."published" = $1 LIMIT $2  [["published", false], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Book id: 2, title: "Agile Web Development with Rails 6", published: false, created_at: "2019-09-28 00:34:09", updated_at: "2019-09-28 00:34:09">]>
```

# Rails 5.2.3 では

symbol (Rails 6 では falsy になる symbol) は truthy な値として扱われます。

# 試したソース

試したソースは以下にあります。
https://github.com/suketa/rails_sandbox/tree/try094_falsy_symbol

# 参考情報

* [What is new in Rails 6.0](https://bogdanvlviv.com/posts/ruby/rails/what-is-new-in-rails-6_0.html)
* [Type cast falsy boolean symbols on boolean attribute as false](https://github.com/rails/rails/pull/35794)