findããfind_by_idã§ActiveRecord::RecordNotFoundãåé¿ãã
æ°ããããã°ãã¯ããã¾ããï¼
ããã°åï¼çèã«ã¡ã¬ãªã³
WordpressãAWSãæã
çãã¬ã«ã¤ãã¦æ¸ãã¦ã¾ãã
Railsã§ãã¼ã¿ãfindããæã該å½ä»¶æ°ãï¼ä»¶ã®æã¯ãActiveRecord::RecordNotFoundãªä¾å¤ã«ãªã£ã¦ãã¾ãã®ã§ã
rescue ActiveRecord::RecordNotFound
ã§ä¾å¤å¦çãããªãã¦ã¯ãªããªãã
ããããã£ã¨é¢åã ãªãã¨ããã£ã¦ããããªãã¨find_by_idã使ãã°ãnilãè¿ãã®ã§
user = User.find_by_id(params[:id]) unless user then # 0件ã®ã¨ãã®å¦ç end
ã¨ä½¿ãããããï¼ï¼
ããããã¯ãfind_by_*** ã使ãã¾ãããã
追è¨
http://blogs.yahoo.co.jp/killer2kmonday/16884102.html
ãã£ã¨ãã³ã³è¦ãããfindã§ä¾å¤ã«ãªãã®ã¯ã
Model.find(id)
ã®ããã«idãæå®ããã¨ãã ãã ã£ãã
Model.find(:first,:conditions=>["name=?",name]) #=> nil
ãããªæãã§ä½¿ãã°nilãè¿ãã®ããã