ActiveRecord::Enumとは

>`ActiveRecord::Enum`とは列挙型あるいは単にenum型とも呼ばれるもので、ActiveRecordが提供するenum型は数値のカラムに対してプログラム上で扱える別名を与えます。

```rb
class AddStatusToBooks < ActiveRecord::Migration[7.0]
  def up
    add_column :books, :sales_status, :integer
  end

  def down
    remove_column :books, :sales_status, :integer
  end
end
```
```rb
class Book < ApplicationRecord
  enum sales_status: {
    reservation: 0,
    now_on_sale: 1,
    end_of_print: 2
  }
end
```

```rb
irb(main):004:1* def book_status(i)
irb(main):005:2*   case i%3
irb(main):006:2*   when 0
irb(main):007:2*     "reservation"
irb(main):008:2*   when 1
irb(main):009:2*     "now_on_sale"
irb(main):010:2*   when 2
irb(main):011:2*     "end_of_print"
irb(main):012:1*   end
irb(main):013:0> end
=> :book_status
irb(main):014:0> book_status(3)
=> "reservation"
```
### 数字が格納されている
```rb
irb(main):019:1* [*1..5].each do |n|
irb(main):020:1*   Book.create(name: "enum Book #{n}", published_on: Date.today.ago(n.hour), price: n*1000,  sales_status: book_status(n))
irb(main):021:0> end
  TRANSACTION (0.3ms)  SAVEPOINT active_record_1
  Book Create (0.5ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 1', '2024-12-12', 1000, '2024-12-13 06:50:42.038654', '2024-12-13 06:50:42.038654', 1)
  TRANSACTION (0.2ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.2ms)  SAVEPOINT active_record_1
  Book Create (0.3ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 2', '2024-12-12', 2000, '2024-12-13 06:50:42.047579', '2024-12-13 06:50:42.047579', 2)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.2ms)  SAVEPOINT active_record_1
  Book Create (0.3ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 3', '2024-12-12', 3000, '2024-12-13 06:50:42.049428', '2024-12-13 06:50:42.049428', 0)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.1ms)  SAVEPOINT active_record_1
  Book Create (0.3ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 4', '2024-12-12', 4000, '2024-12-13 06:50:42.051135', '2024-12-13 06:50:42.051135', 1)
  TRANSACTION (0.2ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.1ms)  SAVEPOINT active_record_1
  Book Create (0.2ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 5', '2024-12-12', 5000, '2024-12-13 06:50:42.052854', '2024-12-13 06:50:42.052854', 2)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
=> [1, 2, 3, 4, 5]
```
### コメントアウトしてみたら関連づけられていないから0になる
```rb
class Book < ApplicationRecord
  # enum sales_status: {
  #   reservation: 0,
  #   now_on_sale: 1,
  #   end_of_print: 2
  # }
end
```

```rb
irb(main):014:1* [*6..10].each do |n|
irb(main):015:1*   Book.create(name: "enum Book #{n}", published_on: Date.today.ago(n.hour), price: n*1000,  sales_status: book_status(n))
irb(main):016:0> end
  TRANSACTION (0.2ms)  BEGIN
  TRANSACTION (0.2ms)  SAVEPOINT active_record_1
  Book Create (0.4ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 6', '2024-12-12', 6000, '2024-12-13 06:56:18.799338', '2024-12-13 06:56:18.799338', 0)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.2ms)  SAVEPOINT active_record_1
  Book Create (0.3ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 7', '2024-12-12', 7000, '2024-12-13 06:56:18.802394', '2024-12-13 06:56:18.802394', 0)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.4ms)  SAVEPOINT active_record_1
  Book Create (0.5ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 8', '2024-12-12', 8000, '2024-12-13 06:56:18.803999', '2024-12-13 06:56:18.803999', 0)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.1ms)  SAVEPOINT active_record_1
  Book Create (0.2ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 9', '2024-12-12', 9000, '2024-12-13 06:56:18.806879', '2024-12-13 06:56:18.806879', 0)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
  TRANSACTION (0.1ms)  SAVEPOINT active_record_1
  Book Create (0.2ms)  INSERT INTO `books` (`name`, `published_on`, `price`, `created_at`, `updated_at`, `sales_status`) VALUES ('enum Book 10', '2024-12-12', 10000, '2024-12-13 06:56:18.808200', '2024-12-13 06:56:18.808200', 0)
  TRANSACTION (0.1ms)  RELEASE SAVEPOINT active_record_1
=> [6, 7, 8, 9, 10]
irb(main):017:0> book_status(6)
=> "reservation"
irb(main):018:0> book_status(7)
=> "now_on_sale"
irb(main):019:0> book_status(8)
=> "end_of_print"
```
> このようにenum型を利用することでコード上ではわかりやすいシンボルなどで指定しつつ、ActiveRecord側ではDB上の定義に合わせて保存することができるようになります

### 名称で数値を指定できるが通常は対応する値を格納させる
>値を気にしない場合はハッシュではなく名称だけの配列を使って暗黙的に列挙型へ割り振られる数値を利用しても良いですが、DBへ保存する値に影響があるため、通常は対応する値を明示しておくのが良いでしょう。

https://gihyo.jp/book/2020/978-4-297-11462-6