Skip to content

Instantly share code, notes, and snippets.

@kyu999
Last active August 29, 2015 14:05
Show Gist options
  • Save kyu999/f0163cf59caea9da38b0 to your computer and use it in GitHub Desktop.
Save kyu999/f0163cf59caea9da38b0 to your computer and use it in GitHub Desktop.
記事検索Tips
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
@kyu999
Copy link
Author

kyu999 commented Aug 27, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment