Railsã§before_validationã³ã¼ã«ããã¯ã使ãæã®è½ã¨ãç©´
TL;DR: before_validation
ã«ç»é²ããã¦ããã³ã¼ããfalse
ãè¿ãã¨ãobj.save
ã¯false
ãè¿ãobj.save!
ã¯ä¾å¤ãæãã¾ããDBã«ã¯ä½ãç»é²ããã¾ããã
Railsã®ã³ã¼ã«ããã¯
便å©ã§ãããï¼ä½¿ã£ã¦ã¾ããï¼
Rails Guidesã®ä¸ç¯ãå¼ç¨ãã¦ã¿ã¾ãã
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
ããªãã¡
ã³ã¼ã«ããã¯ã¯ï¼ActiveRecordã®ï¼ãªãã¸ã§ã¯ãã®ã©ã¤ããµã¤ã¯ã«ã®ä¸ã®ç¹å®ã®æç¹ã§å¼ã³åºãããã¡ã½ããã§ããã³ã¼ã«ããã¯ãå©ç¨ãããã¨ã§ãARãªãã¸ã§ã¯ããä½ãããã¨ãã»ä¿åãããã¨ãã»æ´æ°ãããã¨ãã»åé¤ãããã¨ãã»validationãå®è¡ãããã¨ãã»DBããèªã¿è¾¼ã¾ããã¨ã ãªã©ã«å¿ ãå®è¡ãããã³ã¼ããç»é²ãããã¨ãã§ãã¾ãã
controllerã«ãªãã§ãããã§ãä»»ãã¦ãã¾ãã®ãé²ããmodelã®è²¬åãmodelã«è¨è¿°ãããã¨ãã§ãããããªãã®ã§ã¨ã¦ãæç¨ã§ãã
â»ä¸è¨guidesãreferencesã¨åããã¦ãæ¨å¹´æ«ã®RoR Advent Calendarã«æ稿ããããã®è¨äºãèªãã¨åå¼·ã«ãªãã¾ã â
Ruby - てめえらのRailsはオブジェクト指向じゃねえ!まずはCallbackクラス、Validatorクラスを活用しろ! - Qiita [キータ]
before_validation
ããããæ¬é¡ã§ãã
ä¾é¡ã¨ãã¦ãRailsã§SNSãæ§ç¯ãããã¨ãèãã¾ããã¦ã¼ã¶ã¼ã¯ã¡ã¼ã«ã¢ãã¬ã¹ã«ç´ä»ãã¦ã¢ã«ã¦ã³ããåå¾ããç»é²ããã¨ã¡ã¼ã«ã¢ãã¬ã¹ç¢ºèªã¡ã¼ã«ãéããã¦ãã¾ããã¡ã¼ã«ã¢ãã¬ã¹ã®ç¢ºèªãå®äºããªãã¨ãSNSä¸ã§æ´»åãå§ãããã¨ãã§ãã¾ãããå©ç¨éå§å¾ã«ã¦ã¼ã¶ã¼ãã¡ã¼ã«ã¢ãã¬ã¹ãå¤æ´ããå ´åãã確èªã®ã¡ã¼ã«ãéããã¦ãã¾ãã
# db/migrate/20140104120000_create_users.rb class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :email # some more columns... t.string :email_verification_code # ã¡ã¼ã«ã¢ãã¬ã¹ç¢ºèªç¨ä¹±æ° t.boolean :email_verificated # ã¡ã¼ã«ã¢ãã¬ã¹ç¢ºèªæ¸ã¿æ¨©éãã©ã° end end end
ãã®ããã«ãã¼ãã«ãå®ç¾©ãã以ä¸ã®ãããªã³ã¼ããæ¸ãã¾ãã
# app/models/users.rb class User < ActiveRecord::Base # validationåã«ãemailãå¤æ´ããã¦ããã°ã³ã¼ã«ããã¯é¢æ°ãå¼ã³åºã before_validation :set_email_verification_code, if: 'self.email_changed?' # ä¿åå¾ãã¡ã¼ã«ã¢ãã¬ã¹ç¢ºèªã¡ã¼ã«ãéä¿¡ãã after_save :send_verification_mail, if: 'self.email_changed?' private def set_email_verification_code # æ°ãã確èªç¨ã³ã¼ããçæ self.email_verification_code = SecureRandom.hex(10) # ç¶æ ããæªç¢ºèªãã«ãã self.email_verificated = false end def send_verification_mail # ç¥ end end
ä¸è¦ãã¨ã¦ãèªç¶ã§ãããï¼ãããããã§ã¯ãããããã°ãããSNSãä½ã£ã¦ãã¦ã¼ã¶ã¼ã¯éã¾ãã¾ããããªããã£ã¦ãç»é²ã§ããªãã®ã§ãããï¼
Referenceä¸ã®ä¸æãå¼ç¨ãã¾ãã
If the returning value of a
before_validation
callback can be evaluated tofalse
, the process will be aborted andBase#save
will returnfalse
. IfActiveRecord::Validations#save!
is called it will raise aActiveRecord::RecordInvalid
exception. Nothing will be appended to the errors object.
æ訳ãã¾ãã¨
before_validation
ã³ã¼ã«ããã¯ã®ã³ã¼ããfalse
ï¼ã¾ãã¯false
ã¨è©ä¾¡ããããã®ï¼ãè¿ãã¨ãããã§å¦çãä¸æ¢ããã¾ãï¼validationã¯å®è¡ããã¾ããï¼ãuser.save
ã®éä¸ã§ãããèµ·ããã¨ãuser.save
ã¯åã«falseãè¿ãã¾ãï¼DBã«ã¯ä¿åããã¾ããï¼ãuser.save!
ã®éä¸ã§ãããèµ·ããã¨ãActiveRecord::RecordInvalid
ãèµ·ããã¾ãããããã«ãã¦ããuser.errors
ã«ã¯ãããªãvalidation errorãç»é²ããã¾ããã
ãªãã¨ãããã¨ã§ãããï¼è¦ããä¸ã¯validationã«å¤±æãã¦ããã®ã¨ã¾ã£ããå¤ãããªãã®ã«ãå®ã¯validationãã®ãã®ãã¹ãããããã¦ããã®ã§ãã
ä¸ã§ä½ã£ãUserã¢ãã«ã®ä¾ã§ã¯ãset_email_verification_code
ã®æå¾ã®å¼ã§false
ã代å
¥ãã¦ããããã«ãã¡ã½ãããã®ãã®ã®æ»ãå¤ãfalse
ã«ãªãã¾ããï¼Rubyã§ã¯ä»£å
¥å¼ã®æ»ãå¤ã¯ä»£å
¥ãããå¤ã§ãããï¼
ãããé¿ããã«ã¯åã«ãã®ã¡ã½ããä¸ã®å¼ã®é çªãå ¥ãæ¿ããã°è¯ãã ãã§ãã
def set_email_verification_code # ç¶æ ããæªç¢ºèªãã«ãã self.email_verificated = false # æ°ãã確èªç¨ã³ã¼ããçæ self.email_verification_code = SecureRandom.hex(10) # => æ»ãå¤ãä¹±æ°æååã«ãªã end
Railsãã¼ã ãã©ã®ãããªæå³ã§ãã®æåãè¨è¨ããã®ããããã¾ããããé¥ããããè½ã¨ãç©´ã§ãªããã¤ä¸åº¦ãããã¨æ°ã¥ãã¦æãåºãã®ãå°é£ã§ãï¼æ°ååæ©ã¿ã¾ããï¼ããæ°ãã¤ããã ããã
åå¹´ã»ã©æ¾ç½®ãã¦ããã¾ãããããã¾ãã¦ããã§ã¨ããããã¾ãï¼ç¬
ä»å¹´ããã¤ãã¼ã¹ã«æ´æ°ãã¦ã¾ããã¾ãã®ã§ä½åãããããããããã¾ã