LoginSignup
227
258

More than 5 years have passed since last update.

【Rails】条件に応じてデータを取得する

Last updated at Posted at 2018-09-06

複雑な条件でデータを抽出する

テーブルのデータをより複雑な条件で絞って取得するメソッドをまとめる。

条件式を使用する

基本的な条件式

テーブルのデータを条件式で絞って取得する場合。
データを受け取りたいテーブルに紐づくモデルに対して、
モデル名.where(カラム名: '値')

#例 artistカラムの値が'ももいろクローバーZ' のレコードを取得
@discography = Discography.where(artist: 'ももいろクローバーZ')

#例 artistカラムの値が'ももいろクローバーZ'かつreleased_dateが'2011-07-27' のレコードを取得
@discography = Discography.where(artist: 'ももいろクローバーZ', released_date: '2011-07-27')

not / or メソッド

モデル名.where.not(カラム名: '値')
モデル名.where(カラム名: '値').or(モデル名.where(カラム名: '値'))

#例 artistカラムの値が'ももいろクローバーZ' ではないレコードを取得
@discography = Discography.where.not(artist: 'ももいろクローバーZ')

#例 artistカラムの値が'百田夏菜子' または released_dateの値が'2010-11-10'のレコードを取得
@discography = Discography.where(artist: '百田夏菜子').or(Discography.where(released_date: '2010-11-10'))

ちなみに、andメソッドはないみたいです笑

最初/最後のレコード

モデル名.first
モデル名.last

#例 Discographyテーブルの1番最初のレコードを取得
@discography = Discography.first

#例 Discographyテーブルの先頭から3件のレコードを取得
@discography = Discography.first(3)

#例 Discographyテーブルの1番末尾から4件のレコードを取得
@discography = Discography.last(4)

カラムを指定する

指定したカラムのみのデータを取得する場合。
モデル名.select("カラム名")

#例 Discographyテーブルのnameカラムのデータを取得
@discography = Discography.select('name')
@discography = Discography.select(:name)

#例 Discographyテーブルのnameカラムとartistカラムのデータを取得
@discography = Discography.select('name', 'artist')
@discography = Discography.select(:name, :artist)

指定したカラムの配列を取得

モデル名.pluck("カラム名")

  • selectメソッドを使用した際
    スクリーンショット 2018-09-07 21.54.19.png
    スクリーンショット 2018-09-07 21.55.00.png

  • pluckメソッドを使用した際
    スクリーンショット 2018-09-07 21.55.47.png
    スクリーンショット 2018-09-07 21.56.23.png

レコード順序の並べ替え

順序づけの基準となるカラム名を指定する。
モデル名.order('カラム名')

#例 nameカラムの中身を基準に順序を並べ替えて取得
@discography = Discography.order('name')
@discography = Discography.order('name ASC')
@discography = Discography.order(name: :asc)

#例 nameカラムの中身を基準に逆順序を並べ替えて取得
@discography = Discography.order('name DESC')
@discography = Discography.order(name: :desc)

レコードの個数を指定する

limitメソッドとoffsetメソッドを使用。
モデル名.limit( n )
モデル名.limit( n ).offset( m )

#先頭から3つのレコードを取得
@discography = Discography.limit(3)

#5つ目から3つのレコードを取得
@discography = Discography.limit(3).offset(4)

グループ化する

groupメソッドを使用。group化したいカラム名を入れる。
モデル名.group(カラム名)

#artistカラムでグループ化。
@discography = Discography.group(:artist)

allメソッドで取得した結果を出力すると以下(画像は途中で切れてる)のようになるところ、
スクリーンショット 2018-09-07 21.02.11.png
artistカラムでグループ化して取得した結果が以下。
スクリーンショット 2018-09-07 21.02.45.png

サンプルのテーブルは余裕ができたらもっと誰でも理解しやすいものに置き換えますね、すみません...

空をセット

モデル名.none

破壊的メソッド

オブジェクトの値そのものを変えてくれるもの。
where!, select!, order! など今まで紹介したようなメソッド名に "!" をつける。
効果をわかりやすく紹介したものを下記に示す。

#通常のメソッドで記述すると長くなるが
@discography = Discography.all
@discography = @discography.where(artist: 'ももいろクローバーZ')

#破壊的メソッドを使うと少しすっきり書ける。結果は上と同じになる。
@discography = Discography.all
@discography.where!(artist: 'ももいろクローバーZ')

その他tips

存在・個数・平均・最大・最小・合計を求めた例を以下に示す。

#存在するかどうかをfalse/trueで代入
@exist = Discography.where(released_date: '2011-03-10').exists?

#個数
@exist = Discography.where(released_date: '2011-03-09').count

#平均 (idを平均することはないと思うけど。。)
@exist = Discography.where(released_date: '2011-03-09').average(:id)

#最小
@exist = Discography.where(released_date: '2011-03-09').minimum(:id)

#最大
@exist = Discography.where(released_date: '2011-03-09').maximum(:id)

#合計
@exist = Discography.where(released_date: '2011-03-09').sum(:id)
227
258
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
227
258