pluck

任意のカラムの配列を取得(pluck)

適応バージョン

  • 1.0.0
  • 1.1.0
  • 1.1.1
  • 1.1.6
  • 1.2.0
  • 1.2.6
  • 2.0.0
  • 2.0.1
  • 2.0.3
  • 2.1.0
  • 2.2.1
  • 2.3.2
  • 2.3.8
  • 3.0.0
  • 3.0.5
  • 3.0.7
  • 3.0.9
  • 3.1.0
  • 3.2.3
  • 3.2.8
  • 3.2.13
  • 4.0.0
  • 4.1.0
  • 4.2.1

説明

任意のカラムの配列を取得する

使い方

モデル.pluck(カラム名)

Person.pluck(:id)
# SELECT people.id FROM people
# => [1, 2, 3]
Person.pluck(:id, :name)
# SELECT people.id, people.name FROM people
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
Person.uniq.pluck(:role)
# SELECT DISTINCT role FROM people
# => ['admin', 'member', 'guest']
Person.where(age: 21).limit(5).pluck(:id)
# SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
# => [2, 3]
Person.pluck('DATEDIFF(updated_at, created_at)')
# SELECT DATEDIFF(updated_at, created_at) FROM people
# => ['0', '27761', '173']

ソースコード

ソースコード検索