LoginSignup
2
0

More than 3 years have passed since last update.

Rails6 のちょい足しな新機能を試す80(reselect編)

Posted at

はじめに

Rails 6 に追加された新機能を試す第80段。 今回は、 reselect 編です。
Rails 6 では、 モデルに reselect , reselect! メソッドが追加されました。
rewherereorder があるので、 reselect もあった方が便利じゃないかということで追加されたみたいです。

Ruby 2.6.4, Rails 6.0.0 で確認しました。Rails 6.0.0.rc2 は gem install rails でインストールできます。

$ rails --version
Rails 6.0.0

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

プロジェクトを作る

rails new rails_sandbox
cd rails_sandbox

User モデルを作る

name と email の2つの属性をもつ User モデルを作ります。

bin/rails g model User name email

seed データを作成する

seed データを作成します。

db/seeds.rb
User.create(
  [
    { name: 'Taro', email: 'taro@example.com' },
    { name: 'Jiro', email: 'jiro@example.com' },
    { name: 'Hanako', email: 'hanako@example.com' }
  ]
)

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

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

rails console で確認する

rails console で確認します。

User.select(:name, :email)s1 に代入します。

irb(main):001:0> s1 = User.select(:name, :email)
  User Load (0.2ms)  SELECT "users"."name", "users"."email" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro", email: "taro@example.com">, #<User id: nil, name: "Jiro", email: "jiro@example.com">, #<User id: nil, name: "Hanako", email: "hanako@example.com">]>

irb(main):002:0> s1
  User Load (0.8ms)  SELECT "users"."name", "users"."email" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro", email: "taro@example.com">, #<User id: nil, name: "Jiro", email: "jiro@example.com">, #<User id: nil, name: "Hanako", email: "hanako@example.com">]>

s2 に s1.reselect(:name) を代入します。

irb(main):003:0> s2 = s1.reselect(:name)
  User Load (0.5ms)  SELECT "users"."name" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro">, #<User id: nil, name: "Jiro">, #<User id: nil, name: "Hanako">]>

s2 で発行されるSQL は、 name カラムのみ取得しますが、s1 は nameemail の2つのカラムを取得します。

irb(main):004:0> s2
  User Load (0.6ms)  SELECT "users"."name" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro">, #<User id: nil, name: "Jiro">, #<User id: nil, name: "Hanako">]>

irb(main):005:0> s1
  User Load (0.7ms)  SELECT "users"."name", "users"."email" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro", email: "taro@example.com">, #<User id: nil, name: "Jiro", email: "jiro@example.com">, #<User id: nil, name: "Hanako", email: "hanako@example.com">]>

s1 自体を変更したい場合は、 reselect! を使います。

irb(main):006:0> s1.reselect!(:name)
  User Load (0.6ms)  SELECT "users"."name" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro">, #<User id: nil, name: "Jiro">, #<User id: nil, name: "Hanako">]>
irb(main):007:0> s1
  User Load (0.6ms)  SELECT "users"."name" FROM "users" LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: nil, name: "Taro">, #<User id: nil, name: "Jiro">, #<User id: nil, name: "Hanako">]>

試したソース

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

参考情報

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0