has_oneã®createã¨build and saveã®éã
userã¢ãã«ã¨has_oneã®é¢ä¿ã«ããpassword_resetã¨ããã¢ãã«ãããã
ãã¹ã¯ã¼ããªã»ããã®ã¨ãã«ä½¿ãtokenã¨ã¿ã¤ã ã¹ã¿ã³ããã¨ã£ã¦ããããã®ã¢ãã«ã§userã¢ãã«ã®ä¸é¨ã§ããããã ãã©ããã¾ã«ãã使ããªãè¦ç´ ã¯åãããæ´¾ãªã®ã§åãã¦ããã
å ·ä½çã«ã¯ãããªã³ã¼ãã
class User < ActiveRecord::Base has_one :password_reset, dependent: :destroy ~snip~ class PasswordReset < ActiveRecord::Base before_save { unique_token(self.class, :token) } after_save { UserMailer.password_reset(self.user).deliver! } belongs_to :user ~snip~ end
ã§ãPasswordResetã¯user_idã§ã¦ãã¼ã¯ã«ãªãã
ãã®PasswordResetãä½ãã«ã¯user.create_password_resetããã°ããã ããªãã ãã©ãããã ã¨ã¡ãã£ã¨åé¡ãããããããï¼é£çºããã¨ã¦ãã¼ã¯å¶ç´ã«å¼ã£ããã£ã¦ä¾å¤ãèµ·ãããcreateã¯ãã®åã®éããã¨ã«ããcreateã試ã¿ãã¨ãããã¨ãªã®ããªã
ãã£ã½ãã§saveã¯ç©ºæ°ãèªãã§åãã¦ãããã空æ°ã£ã¦ãããhas_oneãã¡ããã¨ç解ãã¦åä½ãããã¤ã¾ã
user.build_password_reset.save user.build_password_reset.save
ã®ããã«saveãï¼é£çºãã¦ããï¼åãã®saveã®ã¨ãã¯ï¼ã¤åã§ä½ãããpassword_resetãã¼ã¿ãç ´æ£ãã¦æ°ããpassword_resetãçæãã¦ãããã
ãããå®éã«ç¢ºèªãã¦ã¿ãã
irb(main):018:0> PasswordReset.all PasswordReset Load (0.3ms) SELECT `password_resets`.* FROM `password_resets` => [] irb(main):019:0> u.build_password_reset.save (0.2ms) BEGIN SQL (0.2ms) DELETE FROM `password_resets` WHERE `password_resets`.`id` = 3 (0.1ms) COMMIT (0.1ms) BEGIN PasswordReset Exists (0.2ms) SELECT 1 AS one FROM `password_resets` WHERE `password_resets`.`token` = 'jZFhYZXPEgGmKs7f5EUNHg' LIMIT 1 SQL (0.2ms) INSERT INTO `password_resets` (`created_at`, `token`, `updated_at`, `user_id`) VALUES ('2013-06-30 03:51:06', 'jZFhYZXPEgGmKs7f5EUNHg', '2013-06-30 03:51:06', 1) User Load (0.2ms) SELECT `users`.`id`, `users`.`username`, `users`.`email`, `users`.`name`, `users`.`bio`, `users`.`locale`, `users`.`status`, `users`.`avatar_file_name`, `users`.`avatar_content_type`, `users`.`avatar_file_size`, `users`.`avatar_updated_at`, `users`.`created_at`, `users`.`updated_at` FROM `users` WHERE `users`.`id` = 1 LIMIT 1 PasswordReset Load (0.2ms) SELECT `password_resets`.* FROM `password_resets` WHERE `password_resets`.`user_id` = 1 LIMIT 1 (0.5ms) COMMIT => true irb(main):020:0> PasswordReset.all PasswordReset Load (0.3ms) SELECT `password_resets`.* FROM `password_resets` => [#<PasswordReset id: 5, user_id: 1, token: "jZFhYZXPEgGmKs7f5EUNHg", created_at: "2013-06-30 03:51:06", updated_at: "2013-06-30 03:51:06">] irb(main):021:0> u.build_password_reset.save (0.1ms) BEGIN SQL (0.3ms) DELETE FROM `password_resets` WHERE `password_resets`.`id` = 5 (1.6ms) COMMIT (0.1ms) BEGIN PasswordReset Exists (0.2ms) SELECT 1 AS one FROM `password_resets` WHERE `password_resets`.`token` = 'o5s3XR7WYDB3sQBzI9pMlw' LIMIT 1 SQL (0.2ms) INSERT INTO `password_resets` (`created_at`, `token`, `updated_at`, `user_id`) VALUES ('2013-06-30 03:51:21', 'o5s3XR7WYDB3sQBzI9pMlw', '2013-06-30 03:51:21', 1) User Load (0.2ms) SELECT `users`.`id`, `users`.`username`, `users`.`email`, `users`.`name`, `users`.`bio`, `users`.`locale`, `users`.`status`, `users`.`avatar_file_name`, `users`.`avatar_content_type`, `users`.`avatar_file_size`, `users`.`avatar_updated_at`, `users`.`created_at`, `users`.`updated_at` FROM `users` WHERE `users`.`id` = 1 LIMIT 1 PasswordReset Load (0.3ms) SELECT `password_resets`.* FROM `password_resets` WHERE `password_resets`.`user_id` = 1 LIMIT 1 (0.5ms) COMMIT => true irb(main):022:0> PasswordReset.all PasswordReset Load (0.3ms) SELECT `password_resets`.* FROM `password_resets` => [#<PasswordReset id: 6, user_id: 1, token: "o5s3XR7WYDB3sQBzI9pMlw", created_at: "2013-06-30 03:51:21", updated_at: "2013-06-30 03:51:21">] irb(main):023:0>
ã¨ãè¦ã¦ã®éãdeleteãã¦ããinsertãã¦ããã¦ãã
便å©ããã®ãï¼