ã©ãããä»ã¯ä¿å®ãã¡ã¤ã³ã®æ¥åã«ãã¦ãã muryoimpl ã§ãã
ä»åã¯ãç§ããããªã£ã¦ããã¨èªã¿ãããã¦å¬ãããªãã¨ããã³ã¼ãã«ã¤ãã¦ãé å¼µã£ã¦è¨èªåãã¦ããããã¨æãã¾ãã
æ確ãªæéãåºãã«ãããã®ãªã®ã§ããªã¼ããã«ã³ã¼ãããªãã¡ã¯ã¿ãªã³ã° Ruby ã¨ãã£ã·ã§ã³ã«ã¯è¼ã£ã¦ããªããããããªãã§ãããä»ããæ¸ãã¦ãããã¨ãæèããã¦ããã³ã¼ããç§ã¯èªã¿ãããã¨æãã®ã§ããããã観ç¹ãããã®ãããããã®æ°æã¡ã§ãä»ãåããã ããã
ã¡ã½ãããè¦ãã°æµããããããçæè¨æ¶ã«ããããã³ã¼ã
ç§ã¯å±æãè¦ãããããã¾ãå ¨ä½ã®æµããããã£ãããã§è©³ç´°ãèªãã»ããç解ãé²ãã¿ã¤ãã®äººã§ãã ã¡ã½ããã®å ¨ä½ã®æµãããããã¨ãæµãããã©ã®é¨åã§ä½ãè¡ããããã ãããæ³åãããã¨ãã§ããã®ã§ã詳細ãè¦ã«è¡ã£ãã¨ãã«äºãã¤ã¡ã¼ã¸ãæã£ã¦ãããããã¤ã¡ã¼ã¸ãããã°èããç¯å²ãéå®ã§ããããã100%ææ¢ããããå¹çãããã¨æããããã§ãã
æµãããããã¨ããèããç¯å²ãéå®ã§ãããã¨ããã®ãéè¦ã§ãããã®ã¡ã½ããã®ãã¤å½¹å²ã¨æ¯ãèãã«ã ãéä¸ãã¦ãã®ã¡ã½ãããèªãã°ããããã§ããèªã¿é²ãã¦ãããã¡ã«è¨æ¶ãã¦ããã¹ããã¨ãå°ãªãã§ããã¨ãé ã®ä¸ãã¯ãªã¢ãªç¶æ ãä¿ã¦ããããç解ãé²ã¿ã¾ãã
ãã ããèªãã¹ãã¡ã½ãããéå®ã§ããã¨ãã¦ãããã®ã¡ã½ããã極端ã«é·ãã£ãããè¤æ°ãã¡ã¤ã«ã«ã¾ããã£ã¦æ·±ãã¨ããã¾ã§è¿½ã£ã¦ãããªãã¨æçµçãªçµæãä½ããã¦ãããããããªããªã£ã¦ãããããå ´åãè³ã®çæè¨æ¶ããªã¼ãã¼ããã¼ãã¦ãã¾ããããããããªããªã£ã¦ãã¾ãã¾ããããããå ´åãæ»ããã¨ãé£ãããªã£ã¦èªã¿ãªãããã£ã¦ãªããã¨ããããã§ãããã
ä¾ãã°ãä»ã®ãã¡ã¤ã«ã«ããã¡ã½ããã¸ã®åç §ããªã10è¡ã®ã¡ã½ããã¨5,000è¡ã®ã¡ã½ããã§ã¯èªã¿é²ããä¸ã§è¨æ¶ãã¦ãããã¨ã¯ 5,000è¡ã®ã¡ã½ããã®ã»ãããã£ã¨å¤ããªãã¾ããåã 10è¡ã®ã¡ã½ããã§ããåä¸ãã¡ã¤ã«å ã§å®çµãã¦ããã¡ã½ããã¨è¤æ°ã®ãã¡ã¤ã«ã«ã¾ããã£ã¦å®ç¾©ããã¦ããã¡ã½ãããããããçµã¿åããã10è¡ã®ã¡ã½ããã§ã¯å¾è ã®ã»ããè¨æ¶ãã¦ãããã¨ã¯å¤ãã§ãããã
çæè¨æ¶ãé ·ä½¿ããªãè³ãç²ããªãã³ã¼ããèªã¿ãããããããã³ã¼ãã ã¨æãã®ã§ãã
æµãããããã³ã¼ã
ååã¨ãã¦ãã³ã¼ãã¯ä¸ããä¸ã«é ã«å¦çãããããã«ãªã£ã¦ãã¾ããä¸ããä¸ã«èªã¿é²ãã¦ããã¨ããã®ã¯ãæç³»åã«å¦çã追ããã¨ãã§ããã®ã§èªã¿ããããã¨ããã®ã¯åæããã ããã®ã§ã¯ãªããã¨æãã¾ããã¾ããä¸ããä¸ã«èªãã§ããããããªã³ã¼ãã®æ§æã«ãã¾ãããã
ã¡ã½ãããå¤æ°ãæµããç解ã§ããç¨åº¦ã«ãããååãã¤ãã¦ããã¨ãæ´ã«èªã¿ããããªãã¾ããå¤æ°ãã¡ã½ããåã¯å¦çã®ç®æ¬¡ã«çããã¨ç§ã¯æã£ã¦ããã®ã§ãããã¯å¤§äºã§ããæ¦è¦ãååãããããã¨ããã®é¨åã«ã¤ãã¦ã¯èªã/èªã¾ãªãã®å¤æãã¤ããããããç¡é§ã«èª¿ã¹ãå¿ è¦ããªããªããèªã人ã«ã¨ã£ã¦ã¯é常ãããããªãã¾ããååã«ã¤ãã¦ã¯ãªã¼ããã«ã³ã¼ãã®ã第â é¨ è¡¨é¢ä¸ã®æ¹åãã§ããããã¨è§¦ãããã¦ããã®ã§ãã¡ããåç §ãã¦ãã ããã
以ä¸ã®ã³ã¼ã㯠bin/rails g scaffold User
ãã controller ã® create ã¡ã½ããã®ã³ã¼ããä¸é¨å å·¥ãæ½åºãããã®ã§ããuser_params ã¡ã½ãããèªãã®ã«ã¡ãã£ã¨ä¸ã®ã»ãã«ã¸ã£ã³ããã¦æ»ã£ã¦ããªããã°ããã¾ããããåºæ¬çã«ãã¡ã¤ã«ã®ä¸ããä¸ã«èªã¿é²ãã¦ããã¾ãã
class UsersController < ApplicationControllerã def create @user = User.new(user_params) respond_to do |format| if @user.save format.html { redirect_to @user, notice: 'User was successfully created.' } format.json { render :show, status: :created, location: @user } else format.html { render :new } format.json { render json: @user.errors, status: :unprocessable_entity } end end end private def user_params params.fetch(:user, {:first_name, :last_name}) end end
ç§ã¯è¤æ°ã®ãªãã¸ã§ã¯ããç¸äºä½ç¨ãããããªã¡ã½ãããä½æããã¨ãã¯ããã¤ã controller ã®ã¡ã½ãããæãæµ®ãã¹ã¾ãããã®ããã«æµãããããããã«ãªã£ã¦ãããï¼ããããã®å¤æ°ãã¡ã½ããã¯ç®æ¬¡çãªå½¹å²ãæããã¦ãããï¼ã¨èªåããªããå®è£ ãã¦ãã¾ãã
主ã¨ãªããªãã¸ã§ã¯ãã«å¯¾ããæä½ããããããã«æ¸ããã¦ãã
å®ç¾©ãããã¨ãã¦ããã¡ã½ããã®ä¸ã§ã主ã¨ãªããªãã¸ã§ã¯ãã«å¯¾ããæä½ãè¦ããããã«è¨è¿°ã¬ãã«ãæãã¦æ¸ããã¨ãæèããã¨ãå¦çã®æµããè¦ããã³ã¼ãã«ãªãã¾ãã
å
ã»ã©ã® UsersController#create
ã®ä¾ã§ã¯ãUser ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ã主ã¨ãããªãã¸ã§ã¯ãã¨è¨å®ããã¦ãã¾ãããã®ã¡ã½ããã®ä¸ã§æ±ããã¨ãã¦ãã User ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ããæä½ã®æ§åãè¦ããããã«å¦çãæ¸ããã¦ãããããæµãããããããããªã£ã¦ãã¾ãã
ãè¨è¿°ã¬ãã«ãåããããã¨ã¯ããªãã¸ã§ã¯ãã®çæããæä½çµäºã¾ã§ãä¸è²«ãã¦ããã®ã¡ã½ããå ã«æ¸ãã¨ãããã¨ã§ããopen ã close ã®ãããªå¯¾ã«ãªã£ã¦ããå¦çã«ã¤ãã¦ã¯ãå¿ ãåãã¡ã½ããå ã«è¨è¿°ãã¾ããopen ã¯ããã®ã«close ããªãã¨ãæ¬æ¥ã»ããã§ç¾ããã¹ããã®ãç¾ããªãããä¸å®ãç ½ãã¾ãããclose ãæ¢ãå¿ è¦ãåºã¦ãã¦ãã¾ãã¾ãã
å ´åã«ãã£ã¦ã¯å¥ã®ãªãã¸ã§ã¯ã(ä¾: Service ã¯ã©ã¹ã Form Object ãªã©) ã«æ¸¡ãã¦å¦çããé¡ããããã¨ãããã§ããããããã®å ´åã¯ä¸»ã¨ãããªãã¸ã§ã¯ãã«å¯¾ãã¦ä½ããã¦ããã®ããæ確ã«ãããååã«ããå¿ è¦ãããã¾ããå ã«æ¸ããããã«ãå¤æ°ãã¡ã½ããã¯ç®æ¬¡ã«ç¸å½ããã®ã§ååã¯éè¦ã§ããå¿ ãã主ã¨ãããªãã¸ã§ã¯ãã«å¯¾ããæä½ãããããããªååãã¤ãã¾ãããã
ãããã3åã¯ããã³ã°ã«ä¾ãããªãã°ãæçãããéã¯ã¾ãªæ¿ã®ããä½æ¥å°(ä»å®ç¾©ãããã¨ãã¦ããã¡ã½ãã) ã®ä¸ã§ææãæ±ããæé ãã¿ãã¾ããæé ã®ãã¡æéã®ããããã®ã«ã¤ãã¦ã¯ããã¡ãã«äºãæºåãã¦ããããã®ãããã¾ããã¨ã¢ã·ã¹ã¿ã³ã(Serviceã¯ã©ã¹ã Form Object) ã«ä¾é ¼ãã¦ä½ã£ãã¨ãã¦ããå¿ ãã§ãããã£ããããä½æ¥å°ã®ä¸ã§æ±ããã¨ã§å ¨ä½ã®æµãã¨æä½ããããããããªãããã«ãªã£ã¦ãã¾ããããã¨åãã¤ã¡ã¼ã¸ã§ãã 大ããªå¦çãä½ãå ´åã¯ãã¢ã·ã¹ã¿ã³ãã®å¦çãã¢ã·ã¹ã¿ã³ãå´ã§ä½æ¥å°ã®ä¸ã§æä½ããããã«ããã¨ãæ´ã«ãããããããªãã¨æãã¾ãã
ã¾ã¨ã
ä»åã¯ãç§ã®èªã¿ãããã¨æãã³ã¼ãã¨ãããã¨ã§ãå ¨ä½ã®æµãããããã³ã¼ããçæè¨æ¶ãé ·ä½¿ããªãã³ã¼ããç§ã«ã¨ã£ã¦ã¯èªã¿ãããã³ã¼ãã§ããã¨ãããã¨ã表æããã©ãããã°ãããªãã®ãã«ã¤ãã¦å°ãã ã触ãã¦ã¿ã¾ããã
æ§æã ãã§ãªãèªã¿ããã/èªã¿ã«ãããèãã¦ã³ã¼ããæ¸ãã®ã¯å¤§å¤ãªè¡çºãªã®ã§ããããã¡ã¤ã«ãã³ã¼ãã®æ§æä¸åªãã¦ããã¨ãã¦ãèªã¿ã«ããã³ã¼ãã¯ç解ãã«ãããã®ã§ããç解ããã«ããã¨æãå ¥ããã¢ããã¼ã·ã§ã³ãä½ä¸ãã¦ãã¾ãã®ã§ã大å¤ã§ããå°æ¥ä¿®æ£ããèªåã®ããã ã¨æã£ã¦ãã£ã¦ããã¾ãããã