LoginSignup
5
3

More than 5 years have passed since last update.

pluckが常に最速とは限らない

Posted at

属性としてアクセス可能にした関連テーブルはキャッシュされるが、pluckを使うとそのキャッシュが使用されない。

class Member < ActiveRecord::Base
  accepts_nested_attributes_for :groups
# で
m = Member.first # membersからSELECT
m.groups # members inner join groups なクエリの結果がキャッシュされる
m.groups.map{|g| g.id} # 上記の結果からidの配列が作成される
m.groups.pluck(:id) # クエリが再発行される
5
3
1

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
5
3