Rails ã§ãããã 5 ã¤ã®ééã
以ä¸ã®æç« ã¯ãMike Perham æ°ã®ããã°è¨äºã翻訳ãããã®ã§ããåæã¯2012å¹´5æ5æ¥ã«å
¬éããã¾ããã
Five Common Rails Mistakes
http://www.mikeperham.com/2012/05/05/five-common-rails-mistakes/
Railsããããªãã«ä»äºã§ä½¿ã£ã¦ãã¦å¤ãã® Rails ã¢ããªãè¦ã¦ãã¦ãæªã Ruby ã³ã¼ããèªã¿æ¸ãããããã®è¨äºã§ã¯ãã ããããã¹ã¦ã® Rails ã®ã³ã¼ããã¼ã¹ã§è¦ãããå ±éã®ééãã5ã¤æããã
1. ã¹ãã¼ãã®ä»æ§ããªããã¤ã°ã¬ã¼ã·ã§ã³
ãã¼ã¿ã¢ãã«ã¯ã¢ããªã±ã¼ã·ã§ã³ã®ã³ã¢ã ãã¹ãã¼ãã«å¶ç´ããªãã¨ããã¼ã¿ã¯ã³ã¼ããã¼ã¹ã«åå¨ãããã°ã«ããå¾ã
ã«èã¾ãã¦ããããã£ã¼ã«ãã«å¤ãå
¥ã£ã¦ãããä¿¡é ¼ã§ããªããªããããã« Contact ã¹ãã¼ããããã¨ãããã
create_table "contacts" do |t| t.integer "user_id" t.string "name" t.string "phone" t.string "email" end
ããã«ä½ãå¿ è¦ã ãããï¼ãããããUser ã«æå±(belogs_to)ãã¦ãã¦ã1ã¤ã®ååãæã£ã¦ããã ããããã¼ã¿ãã¼ã¹ã®å¶ç´ã使ã£ã¦ãããä¿è¨¼ãããã:null => false ã追å ãã¦ããã°ãããªãã¼ã·ã§ã³ã«ãã°ããã£ãã¨ãã¦ãããã¼ã¿ãã¼ã¹ãã¢ãã«ã®ä¿åã許ããªãã®ã§ãã¢ãã«ã常ã«ä¸è²«æ§ãä¿ã¦ãã
create_table "contacts" do |t| t.integer "user_id", :null => false t.string "name", :null => false t.string "phone" t.string "email" end
ãã¼ãã¹ãã¤ã³ã
limit => N ã使ã£ã¦ãstring åã®ã«ã©ã ã大ãããæ£ç¢ºã«æ±ºãã¦ãããã string ã¯ããã©ã«ãã§255æåã§ãé»è©±çªå·ã«ã¯ããã ãã®å¤§ããã¯å¿
è¦ããªãã
2. ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°
ã»ã¨ãã©ã® Rails ããã°ã©ãã¯ãªãã¸ã§ã¯ãæåã® Ruby ã³ã¼ããæ¸ãã¦ããªããMVC æåã® Ruby ã³ã¼ããæ¸ãã¦ã¢ãã«ã¨ã³ã³ããã¼ã©ãæå®ã®å ´æã«é
ç½®ãããããã¦ãã¯ã¦ã¼ãã£ãªãã£ã¢ã¸ã¥ã¼ã«ã«ã¯ã©ã¹ã¡ã½ãããæ¸ã㦠lib/ ã«è¿½å ããã2ã3å¹´ãã¦éçºè
ã¯æ°ã¥ããã Rails ã Ruby ãªãã ãRails ãæ示ãã¦ä¿è¨¼ãã¦ããªãããæ¹ã§ããã·ã³ãã«ãªãªãã¸ã§ã¯ããä½ã£ã¦çµã¿ç«ã¦ã¦ãããã ï¼ãã¨ã
ãã¼ãã¹ãã¤ã³ã
ãµã¼ããã¼ãã£ã®ãµã¼ãã¹ãå¼ã³åºãã®ã«ãã¡ãµã¼ããå°å
¥ãããããã¹ãã³ã¼ãã«ã¢ãã¯ã»ãã¡ãµã¼ããæä¾ãããã¹ãã¹ã¤ã¼ãã§ã¯ãµã¼ããã¼ãã£ã®ãµã¼ãã¹ãå®éã«å¼ã°ãªãããã«ã§ããã
3. ãã«ãã¼ã§ HTML ãçµåãã
ãã«ãã¼ã¡ã½ãããä½ã£ã¦ããã°ãæä½éãã¥ã¼å±¤ããããã«(clean)ãããã¨ãã¦ããã¨ãããã¨ãªã®ã§ãè³è³ããããããããéçºè
ã¯ãã«ãã¼å
é¨ã§ã¿ã°ãä½ãéã®åºæ¬ãç¥ããªããã¨ãå¤ããæååããããªãçµåãããæ¹å¤ããããããã¨ã«ãªãã
str = "<li class='vehicle_list'> " str += link_to("#{vehicle.title.upcase} Sale", show_all_styles_path(vehicle.id, vehicle.url_title)) str += " </li>" str.html_safe
ãããããã¯éããXSS ã®ã»ãã¥ãªãã£ãã¼ã«ã容æã«çãã§ãã¾ãï¼content_tagãããåéã ã
content_tag :li, :class => 'vehicle_list' do link_to("#{vehicle.title.upcase} Sale", show_all_styles_path(vehicle.id, vehicle.url_title)) end
ãã¼ãã¹ãã¤ã³ã
ãããã¯ãã¨ããã«ãã¼ã¡ã½ãããå°å
¥ãããããã¹ããããããã¯ã¯ãã¹ããã HTML ã«éåæãªãåãã
4. ãã¹ã¦ãã¡ã¢ãªã«ãã¼ããã巨大ãªã¯ã¨ãª
ãã¼ã¿ãè¨æ£ããå¿
è¦ãããå ´åããã¹ã¦ãã¤ãã¬ã¼ã¿ã§åãã¦ä¿®æ£ããã®ã§ã¯ãªãããªï¼
User.has_purchased(true).each do |customer| customer.grant_role(:customer) end
100 ä¸ã¦ã¼ã¶ãæ±ãã EC ãµã¤ããéå¶ãã¦ããå ´åã1 ã¦ã¼ã¶ã 500 ãã¤ãã ã£ããã©ãã ããï¼ãã®ã³ã¼ãã¯ã©ã³ã¿ã¤ã ã®ã¡ã¢ãªã« 500MB ã使ã£ã£ã¦ãã¾ãï¼ ããè¯ãæ¸ãæ¹ã¯ããã ã
User.has_purchased(true).find_each do |customer| customer.grant_role(:customer) end
find_each 㯠find_in_batches ã使ã£ã¦ãã¦ãä¸åº¦ã«å¼ã£å¼µã£ã¦ããã®ã¯ 1000 ã¬ã³ã¼ããªã®ã§ãã©ã³ã¿ã¤ã ã®ã¡ã¢ãªè¦ä»¶ãåçã«ä¸ãã¦ãããã
ãã¼ãã¹ãã¤ã³ã
update_all ã çã® SQL ã使ã£ã¦ã大éæ´æ°ã®ããã©ã¼ãã³ã¹ãä¸ããã SQL ã®å¦ç¿ã«ãã£ãããªæéããããããããã ãå©ç¹ã¯æ°å¤ããããããã©ã¼ãã³ã¹ã 100 åæ¹åã§ããã ããã
5. ã³ã¼ãã¬ãã¥ã¼ï¼
Github ã使ã£ã¦ããã¨æãããpull request ã¯ä½¿ã£ã¦ããªãã¨æãã1ã2æ¥ä½¿ã£ã¦æ©è½ã追å ãããªãããã©ã³ãä¸ã§ä½æ¥ãã¦pull request ãéããããã¼ã ãåã®ã³ã¼ããã¬ãã¥ã¼ã§ããããã«ãªããæ¹åç¹ã¨èãã¦ããªãã£ãã¨ãã¸ã±ã¼ã¹ã®ææ¡ãå¾ãããããã®ãããã§åã®ã³ã¼ããããé«ãå質ãæã¤ãã¨ãä¿è¨¼ãããåã㯠TheClymb ã®å¤æ´ã®90%ã pull request ã§è¡ã£ã¦ãã¦ãããã㯠100% ãã¸ãã£ããªä½é¨ã ã£ãã
ãã¼ãã¹ãã¤ã³ã
æä½éããããã¼ãã¹(happy path)ãããã¯ãã¹ãã³ã¼ããªã㧠pull request ããã¼ã¸ãããããªããã¨ããã¹ãã£ã³ã°ã¯ã¢ããªã±ã¼ã·ã§ã³ãå®å®ããã¦ãå¹³ç©ã«ç ãããã«ã¯ä¸å¯é¿ã ã
åãããããåé¡ãè¦éãã¦ãããã³ã¡ã³ãã§ç¥ããã¦ï¼
æ´æ°: Mike Perham æ°ãã許諾é ãã¾ããããã·ã¢èªã«ã翻訳ããã¦ãã¾ããã