{"id":8277,"date":"2022-06-16T06:13:55","date_gmt":"2022-06-15T21:13:55","guid":{"rendered":"https:\/\/yumishin.com\/?p=8277"},"modified":"2022-06-16T06:13:55","modified_gmt":"2022-06-15T21:13:55","slug":"transaction","status":"publish","type":"post","link":"https:\/\/yumishin.com\/?p=8277","title":{"rendered":"\u3010rails\u30fbtransaction\u3011user\u306ecreate\u3067\u3001user_save!\u5468\u308a\u306btransaction\u3092\u8a2d\u5b9a\u3059\u308b\u30d1\u30bf\u30fc\u30f3"},"content":{"rendered":"<p>\u3069\u3046\u3082\u3001\u4eca\u56de\u306ftransaction\u306b\u3064\u3044\u3066\u5c11\u3057\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002\u4eca\u307e\u3067\u3001transaction\u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u306a\u304f\u3001\u7d50\u69cb\u3042\u305f\u3075\u305f\u3057\u305f\u306e\u3067\u3001\u3001\u3001\u3002<\/p>\n<h2 id=\"i-0\">\u3010rails\u30fbtransaction\u3011user\u306ecreate\u3067\u3001user_save!\u5468\u308a\u306btransaction\u3092\u8a2d\u5b9a\u3059\u308b\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>&nbsp;<\/p>\n<h3 id=\"i-1\">\u7d50\u8ad6\u3068\u30dd\u30a4\u30f3\u30c8<\/h3>\n<p>\u7d50\u9732\u3068\u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>transaction\u3067\u56f2\u3046\u306e\u306f\u3001SQL\u304c\u6d41\u308c\u308b\u51e6\u7406\u306e\u307f\n<ul>\n<li>\u4f8b\u3048\u3070user.save!\u306a\u3069<\/li>\n<li>SQL\u304c\u6d41\u308c\u306a\u3044\u51e6\u7406\u3092\u56f2\u3046\u306e\u306fNG<\/li>\n<\/ul>\n<\/li>\n<li>transaction\u306f\u4f8b\u5916\u3092\u767a\u751f\u3059\u308b\u51e6\u7406\u3092\u56f2\u3046\n<ul>\n<li>\u4f8b\u5916\u3092\u767a\u751f\u3057\u306a\u3044\u5834\u5408\u306f\u5fc5\u8981\u306a\u3057<\/li>\n<li>\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u305f\u3081\u3001\u4f8b\u5916\u306b\u5bfe\u5fdc\u3059\u308b\u51e6\u7406\u304c\u5fc5\u8981\u306b\u306a\u308b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u304c\u3042\u3063\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> controllers\/users_contoroller.rb<\/span><\/p>\n<pre><code>def create\n  @user = User.new(user_params)\n\n  if @user.save!\n    @user.invite!(current_user)\n    redirect_to @user\n  else\n    render :new\n  end\nend<\/code><\/pre>\n<\/div>\n<p>\u3053\u306e\u6642\u306b\u3001user_save!\u3068user_invite!\u3092\u8003\u3048\u308b\u3068\u3001\u3001\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7406\u7531\u304b\u3089transaction\u3092\u8a2d\u5b9a\u3057\u305f\u65b9\u304c\u3088\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n<ul>\n<li>user_save!\u306fuser\u306e\u60c5\u5831\u3092\u4fdd\u5b58\u3059\u308b\u51e6\u7406<\/li>\n<li>user.invite!(current_user)\u306f\u3001\u73fe\u5728\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308bcurrent_user\u304c\u3001\u65b0\u3057\u304f\u4f5c\u3089\u308c\u305fuser\u306b\u62db\u5f85\u30e1\u30fc\u30eb\u3092\u9001\u3063\u3066\u3001\u65b0\u3057\u3044user\u304c\u62db\u5f85\u30e1\u30fc\u30eb\u306e\u30ea\u30f3\u30af\u3092\u8e0f\u3093\u3067\u8a8d\u8a3c\u3055\u308c\u305f\u3089\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u8a2d\u5b9a\u306a\u3069\u304c\u3067\u304d\u308b\u51e6\u7406<\/li>\n<li>\u3082\u3057user_save!\u306b\u3088\u3063\u3066user\u304c\u4fdd\u5b58\u3055\u308c\u305f\u3068\u3057\u3066\u3082\u3001user_invite!\u306e\u51e6\u7406\u304c\u5931\u6557\u3057\u305f\u6642\uff08\u4f8b\u3048\u3070mail\u304c\u65b0\u3057\u3044user\u306b\u5c4a\u304b\u306a\u304b\u3063\u305f\u5834\u5408\uff09\u3001\u305d\u306euser\u306f\u4f7f\u308f\u308c\u308b\u3053\u3068\u304c\u306a\u3044\u30e6\u30fc\u30b6\u30fc\u3068\u3057\u3066\u6b8b\u3063\u3066\u3057\u307e\u3046\u3002<\/li>\n<li>\u306a\u306e\u3067\u3001user_save!\u3068user_invite!\u306e\u4e21\u65b9\u304c\u6210\u529f\u3057\u3066\u3044\u3066\u307b\u3057\u3044\u3001\u3082\u3057\u3069\u3061\u3089\u304b\u304c\u30df\u30b9\u308c\u3070\u3001\u3069\u3063\u3061\u3082\u3084\u3089\u306a\u304b\u3063\u305f\u3053\u3068\u306b\u3057\u3066\u307b\u3057\u3044<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u308f\u3051\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30b3\u30fc\u30c9\u3092\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> controllers\/users_controller.rb<\/span><\/p>\n<pre><code>def create\n  @user = User.new(user_params)\n  # transaction\u3067\u56f2\u3080\u306e\u306fSQL\u304c\u6d41\u308c\u308b\u51e6\u7406\u3060\u3051\u304c\u30d9\u30b9\u30c8\n  ActiveRecord::Base.transaction do\n    @user.save!\n    @user.invite!(current_user)\n  end\n  redirect_to @user #\u3000transaction\u304c\u6210\u529f\u3057\u305f\u3089\u305d\u306e\u307e\u307euser\u60c5\u5831\u3092\u8868\u793a\nrescue :e # \u3082\u3057transaction\u304c\u5931\u6557\u3059\u308b\u3068\u3001rescue\u3055\u308c\u3066\u3001\u4f8b\u5916\u306e\u60c5\u5831\u304ce\u306b\u5165\u308b\n  render :new # \u5931\u6557\u3057\u305f\u5f8c\u306f\u3001new\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u8868\u793a\nend<\/code><\/pre>\n<\/div>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3046\u3082\u3001\u4eca\u56de\u306ftransaction\u306b\u3064\u3044\u3066\u5c11\u3057\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002\u4eca\u307e\u3067\u3001transaction\u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u306a\u304f\u3001\u7d50\u69cb\u3042\u305f\u3075\u305f\u3057\u305f\u306e\u3067\u3001\u3001\u3001\u3002 \u3010rails\u30fbtransaction\u3011user\u306ecreate\u3067\u3001user_ &#8230; <\/p>\n","protected":false},"author":1,"featured_media":8278,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256],"tags":[],"class_list":["post-8277","post","type-post","status-publish","format-standard","has-post-thumbnail","category-it","entry"],"_links":{"self":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8277"}],"version-history":[{"count":1,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8277\/revisions"}],"predecessor-version":[{"id":8279,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8277\/revisions\/8279"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/media\/8278"}],"wp:attachment":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}