LoginSignup
0
2

More than 5 years have passed since last update.

where メソッド 検索条件

Last updated at Posted at 2017-02-26

where'(:closed => true):条件を表すハッシュ'

model_controller.rb(query_whereアクション)
#closed列がtrueである記事を取得
@aricles = Article.where(:closed => true)
    SELECT "articles".* FROM "articles" WHERE "articles"."closed" = 't'
#category列が「Script」で、かつ、closed列がfalseである記事を取得
@aricles = Article.where(:category => 'Script' AND "articles"."closed" = 'f')
    SELECT "articles".* FROM "articles"
    WHERE "articles"."category" = 'Script' AND "articles"."closed" = 'f'
#published列が2011-01-01~2011-06-30の間である記事を取得
@articles = Article.where(:)published => '2011-01-01'..'2011-06-30'
    SELECT "articles".* FROM "articles"
    WHERE ("articles". "published" BETWEEN '2011-01-01' AND '2011-06-30')
#category列が「.NET」または「Script」である記事を取得
@articles + Article.where(:category => ['.NET','Script'])
    SELECT "articles".* FROM "articles"
    WHWRE "articles". "category" IN ('.NET', 'Script')
model_controller.rb(query_placeアクション)
#category列が「Script」、published列が2011-01-01以降の記事を取得
@articles = Article.wher('category = ? AND published > ?',
    'Script', '2010-01-01')
#同じ条件式を名前付きパラメータで書き換えたもの
@aricles = Article.where(
'category = :category AND published > :published',
{ :category => 'Script', :published => '2011-01-01' })
----------
SELECT "".* FROM "articles"
WHERE (category + 'Script' AND published > '2010-01-01' )
0
2
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
2