LoginSignup
1
1

More than 5 years have passed since last update.

db内の(指定)データの確認exist?

Posted at

db内のデータの確認exist?(引数も可能)

使い方

  モデル.exist?(条件)

例:

Pagesテーブルに1件でもデータは存在するか確認
*引数を渡さないで使用

Page.exists?
# SELECT 1 FROM "pages" LIMIT 1

categoryがrailsであるデータが存在するか確認

Page.exists?(:category => "rails")
# SELECT 1 FROM "pages" WHERE "pages"."category" = "rails" LIMIT 1
1
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
1
1