記事検索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