update_attribute ã¨update_attribtues ã®éã
以åã«æ¸ããã³ã¼ããèªãã§ããã¨ã1ã«ã©ã ã®å¤ã ããå¤ããããã¨ãã£ã¦ãupdate_attribute ã¡ã½ããã使ã£ã¦ããé¨åããã£ããããã¯ãã¾ããããªã
update_attribute ã¨update_attribtues ã®éã
åè
ã¯1ã¤ã®ã«ã©ã ã®å¤ããå¾è
ã¯è¤æ°ã®ã«ã©ã ã®å¤ãæ´æ°ã§ãããã¨ããéã以ä¸ã«ããupdate_attribute ã¡ã½ããã¯validation ãªãã«æ´æ°ãupdate_attributes ã¡ã½ããã¯validation ããã§æ´æ°ãã¨ããéããããã¾ãã
ã¤ã¾ããupdate_attribute 使ãã¨ãæ¤è¨¼å¦çãªãã«ãå¤ãæ´æ°ããã¦ãã¾ããã½ã¼ã¹ãè¦ãã¨ãupdate_attribute ã®æ¹ã¯ãsave(false) ã使ã£ã¦æ´æ°ãã¦ããã®ãåããã
# Updates a single attribute and saves the record without going through the normal validation procedure. # This is especially useful for boolean flags on existing records. The regular +update_attribute+ method # in Base is replaced with this when the validations module is mixed in, which it is by default. def update_attribute(name, value) send(name.to_s + '=', value) save(false) end # Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will # fail and false will be returned. def update_attributes(attributes) self.attributes = attributes save end
update_attribute ããããªé¢¨ã«ä½¿ãã¨ãã°ã
- 1ã«ã©ã ã ãã®æ´æ°ã ãã©ãå¤ã¯ã¯ã©ã¤ã¢ã³ãããéããã
def update_age @user.update_attiribute(:age, params[:age]) end
params[:age] ã«ãã»ãã»ããã¨ãéããã¦ãã¦ãæ´æ°ããã¦ãã¾ãã
- ãã¹ãã±ã¼ã¹ã®çºã«å¤ãå¤æ´ããå ´å
user = users(:bob) user.update_attribute(:age, 23)
ãã®å ´åããä¸æ£ãªå¤ãå
¥ãã¦ãæ´æ°ããã¦ãã¾ãã¾ããéã«ããã£ã¯ã¹ãã£ãã¼ã¿èªä½ãããããï¼AR#valid? ãfalse ãè¿ãï¼ã¨ããã¨ãã§ããªãç¶æ
ã«ãªã£ã¦ã¦ãã¹ã«ã¼ããã¦æ°ãã¤ããªãã
update_attribute ã使ãéã«ã¯æ³¨æ
ä¸ã®ã¾ããä¾ã®å ´åããsaveãã¡ã½ããã使ãããã«æ¸ãæããæ¹ãããã¨æãã¾ããã¾ãã¯ããupdate_attibutesãã¡ã½ããã使ãã
ã¢ãã«ã®ã¡ã½ããçãã¦ã¼ã¶ããã®å¤ã«ä¾åããªãå¤æ´ãéãå ´åã«ã¯ä½¿ã£ã¦ããããã¨æãã¾ãã
def inactive! update_attibute(:active, false) end