LoginSignup
0
1

More than 3 years have passed since last update.

【Rails】 .where について

Posted at

はじめに

.whereについて調べたことについて自分なりにまとめておきます。

.where

Post.where(title: "food")

titleがfoodのレコードを全て取得する

.where.not

NOT条件

Post.where.not(title: "food")

titleがfood以外のレコードを全て取得する

.where.and

AND条件

Post.where(title: "food", price: 1000)

titleがfood、なおかつpriceが1000のレコードを全て取得する

.where.or

OR条件

Post.where(title: "food").or(where(price: 1000)

titleがfood、またはpriceが1000のレコードを全て取得する

AND条件OR条件

Post.where(title: "food", price: 1000).or(Post.where(title: drink))

・titleがfood、なおかつpriceが1000のレコード
・またはtitleがdrinkのレコード

 を取得する

まとめ

・指定したレコード全てを取得する

・NOT条件
 O以外のレコード全てを取得する

・AND条件
 OなおかつXのレコードを取得する

・OR条件
 OまたはXのレコードを取得する

0
1
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
0
1