Last active
August 29, 2015 14:05
-
-
Save kyu999/f0163cf59caea9da38b0 to your computer and use it in GitHub Desktop.
記事検索Tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://railsdoc.com/references/find | |
http://www.atmarkit.co.jp/ait/articles/1405/30/news036_2.html#03 | |
超使える解説 => http://qiita.com/budougumi0617/items/d98fc15adea4dab438e7 | |
1. idで取得 => 1つのレコードを取得; | |
=> モデル.find(引数) | |
2. ageが30以上のすべてのレコードを取得 | |
=> users = User.where("age >= ?", 30) | |
3. sqlインジェクション: 対策外部から入力された値を使う場合 | |
=> params[:user]user = User.find(:all, :conditions => ["name = ?", name]) | |
前方一致 => | |
class Project < ActiveRecord::Base | |
def self.search(search) #self.でクラスメソッドとしている | |
if search # Controllerから渡されたパラメータが!= nilの場合は、titleカラムを部分一致検索 | |
Project.where(['name LIKE ?', "%#{search}%"]) | |
else | |
Project.all #全て表示。 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これもいい。http://www.atmarkit.co.jp/ait/articles/1405/30/news036_2.html#03