ãRailsãã¢ãã«ã®ã¢ãããã¼ãæã«å¤æ´ãããã«ã©ã ãåå¾ããæ¹æ³
Controllerã®editããã³updateã¢ã¯ã·ã§ã³ãªã©ã§ãã¼ã¿ã®ã¢ãããã¼ããè¡ã£ãéãå¤æ´ããããã¼ã¿ã®åå¾ãããããã¨ãããã¾ãããã
ã¨ããããã§åå¾æ³ãã¡ã¢ã£ã¦ããã¾ãã
å¤æ´é¢é£ã®ã¡ã½ãã群
Ruby On Rails ピチカート街道 - Rails 2.1・その5(model の値変更を覚えている) -
ãã¡ãã®æ¹ãç´ æ´ãããçºãã¦ãã ãã£ã¦ããã®ã§ã詳ããã¯ãã¡ãã§ç¢ºèªãã¦é ããã°ã¨æãã¾ãã
ä»å使ç¨ãã¦ããã®ã¯
SomeModel.changes
ã¨ããã¡ã½ããã§ãããã¯å¤æ´ãããã«ã©ã ã®ã«ã©ã åã¨åå¾ã®ãã¼ã¿ãé
åã§è¿ãã¦ããããã®ã§ãã
è¿ã£ã¦ããå¤ã¯ãããªæãã
{"name"=>["hogehoge", "fugafuga"], "age"=>["17", "35"]", updated_at"=>[Mon, 11 Aug 2014 22:23:57 JST +09:00, Mon, 11 Aug 2014 22:27:29 JST +09:00]}
â»é åã¯ã¤ã¡ã¼ã¸ã§ã
以ä¸ã§æ©éå®è£
ã
å¤æ´ã«ã©ã ã®åå¾
- SomeModel
class SomeModel < ActiveRecord::Base after_update :get_changes attr_accessor :change_cols private def get_changes @change_cols = self.changes end end
after_updateã«changesãè¡ãã¡ã½ãããè¨å®ãããã®å¤æ´ãæ ¼ç´ããããã®ã¯ã©ã¹å¤æ°ãç¨æããã ãã§ããé常ã«ç°¡åï¼
ãã¨ã¯ã³ã³ããã¼ã©ã§Updateãå¼ãã§ãããã°ã該å½ã¢ãã«ã®ã¢ã¯ã»ããµã§å®ç¾©ããã¯ã©ã¹å¤æ°ã«å¤æ´ãããã«ã©ã ã®æ
å ±ãæ ¼ç´ããã¾ãã
- SomeController
class SomeController < ApplicationController def update @some_instance = SomeModel.find(some_id) @some_instance.update!(some_params) # @some_instance.change_colsã§å¤æ´ã«ã©ã ã®é åã«ã¢ã¯ã»ã¹å¯è½ end
ãããªæãã§ç¡äºå¤æ´ã«ã©ã ã®åå¾ãåºæ¥ã¾ããã
ãã¨ã¯viewã§ä½¿ããªããã¡ã¼ã«ã®ã¡ãã»ã¼ã¸ã«è¼ãããªãã好ãã«å©ç¨ããã°ããã§ãã