Help us understand the problem. What is going on with this article?

Rails6 のちょい足しな新機能を試す79(enum negative scope編)

はじめに

Rails 6 に追加された新機能を試す第79段。 今回は、 enum negative scope 編です。
Rails 6 では、 モデルで enum を使ったとき、 negative scope が定義されるようになりました。
Ruby 2.6.4, Rails 6.0.0 で確認しました。Rails 6.0.0 は gem install rails でインストールできます。

$ rails --version
Rails 6.0.0

今回は、 Child モデルを作成して rails console を使って確認します。

プロジェクトを作る

rails new rails_sandbox
cd rails_sandbox

Child モデルを作る

name と generation の2つの属性をもつ Child モデルを作ります。
generation は enum にするため、 integer にします。

bin/rails g model Child name generation:integer

enum を定義する

Child モデルに enum を定義します。

app/models/child.rb
class Child < ApplicationRecord
  enum generation: %i[baby toddler preschool gradeschool teen young_adult]
end

seed データを作成する

seed データを作成します。

db/seeds.rb
Child.create(
  [
    { name: 'Andy', generation: :baby },
    { name: 'Bob', generation: :toddler },
    { name: 'Cindy', generation: :preschool }
  ]
)

マイグレーションを実行し seed データを登録する

bin/rails db:create db:migrate db:seed

rails console で確認する

rails console で確認します。

Child.not_baby を実行してみます。

irb(main):001:0> Child.not_baby
  Child Load (0.3ms)  SELECT "children".* FROM "children" WHERE "children"."generation" != $1 LIMIT $2  [["generation", 0], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Child id: 2, name: "Bob", generation: "toddler", created_at: "2019-08-31 00:54:50", updated_at: "2019-08-31 00:54:50">, #<Child id: 3, name: "Cindy", generation: "preschool", created_at: "2019-08-31 00:54:50", updated_at: "2019-08-31 00:54:50">]>

Child.not_baby.not_toddler を実行してみます。

irb(main):003:0> Child.not_baby.not_toddler
  Child Load (0.5ms)  SELECT "children".* FROM "children" WHERE "children"."generation" != $1 AND "children"."generation" != $2 LIMIT $3  [["generation", 0], ["generation", 1], ["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Child id: 3, name: "Cindy", generation: "preschool", created_at: "2019-08-31 00:54:50", updated_at: "2019-08-31 00:54:50">]>

こちらもちゃんと動作しました。

試したソース

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

参考情報

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away