{"id":13089,"date":"2021-02-06T10:02:19","date_gmt":"2021-02-06T01:02:19","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13089"},"modified":"2023-05-27T22:39:15","modified_gmt":"2023-05-27T13:39:15","slug":"%e3%80%90rails%e3%80%91%e3%80%8cfind_or_initialize_by%e3%80%8d%e3%82%84%e3%80%8cfind_or_create_by%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13089","title":{"rendered":"\u3010Rails\u3011\u300cfind_or_initialize_by\u300d\u3084\u300cfind_or_create_by\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>\u300cfind_or_initialize_by\u300d\u3084\u300cfind_or_create_by\u300d\u3068\u306f\uff1f<\/h2>\n<p>Rails4\u4ee5\u4e0a\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u305fActiveRecord\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h2>find_or_initialize_by<\/h2>\n<p>\u65b0\u898f\u4f5c\u6210\u3092\u3057\u3066\u4fdd\u5b58\u3057\u306a\u3044\u3002\u65e2\u5b58\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u304c\u3042\u308c\u3070find_by\u306e\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u3066\u3001\u306a\u3051\u308c\u3070new\u3057\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h3>\u7528\u9014<\/h3>\n<p>\u65e2\u306b\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u30ec\u30b3\u30fc\u30c9\u304b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u9664\u53bb\u3059\u308b\u305f\u3081\u306b\u4f7f\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001<span style=\"color: #ff0000;\"><strong>\u30d0\u30c3\u30c1\u51e6\u7406\u306e\u4e00\u62ec\u767b\u9332\u6642\u306b\u5c11\u3057\u3067\u3082\u51e6\u7406\u306e\u8ca0\u8377\u3092\u8efd\u6e1b\u3057\u305f\u308a\u3059\u308b\u305f\u3081<\/strong><\/span>\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h3>\u6ce8\u610f\u70b9<\/h3>\n<p>\u521d\u671f\u5316\u3059\u308b\u306e\u3068save!\u3059\u308b\u306e\u304c\u5225\u306b\u884c\u308f\u308c\u3066\u3044\u308b\u306e\u3067\u3082\u3057\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30b9\u74b0\u5883\u3067\u4e26\u5217\u3067\u52d5\u304b\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u30e6\u30cb\u30fc\u30af\u5236\u7d04\u304c\u304b\u304b\u3063\u3066\u3044\u308b\u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u3064\u3044\u3066\u540c\u3058\u5024\u3092\u6301\u3063\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u521d\u671f\u5316\u3055\u308c\u3066\u3069\u3061\u3089\u304b\u304c\u5148\u306bsave!\u3055\u308c\u308b\u3068\u30e6\u30cb\u30fc\u30af\u5236\u7d04\u306b\u5f15\u3063\u304b\u304b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002\u5bfe\u7b56\u3068\u3057\u3066\u306f\u30e6\u30cb\u30fc\u30af\u5236\u7d04\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u5148\u306b\u884c\u3046\u3088\u3046\u306b\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h3>find_or_initialize_by\u306e\u623b\u308a\u5024 = persisted?<\/h3>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4fdd\u5b58\u72b6\u614b\u3084\u771f\u507d\u5024\u3092\u30c1\u30a7\u30c3\u30af\u3067\u304d\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4fdd\u5b58\u3055\u308c\u3066\u3044\u306a\u3044\u304b\u3064\u4eca\u307e\u3067\u306b\u524a\u9664\u3055\u308c\u3066\u3044\u306a\u3044\u6642\u3060\u3051true\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h3>find_or_initialize_by\u306e\u623b\u308a\u5024 = new_record?<\/h3>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4fdd\u5b58\u72b6\u614b\u3084\u771f\u507d\u5024\u3092\u30c1\u30a7\u30c3\u30af\u3067\u304d\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4fdd\u5b58\u3055\u308c\u3066\u3044\u306a\u3044\u6642\u3060\u3051true\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h2>find_or_create_by<\/h2>\n<p>\u5bfe\u8c61\u3092\u691c\u7d22\u3057\u3066\u306a\u3044\u5834\u5408\u306fcreate\u3059\u308b\u3002(find_or_initialize_by\u306fnew\u3067\u3059\u304c\u3053\u3061\u3089\u306fcreate\u3067\u3059\u3002)<\/p>\n<p>\u65b0\u898f\u4f5c\u6210\u3092\u3057\u3066\u4fdd\u5b58\u3059\u308b\u3002\u65e2\u5b58\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u304c\u3042\u308c\u3070find_by\u306e\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u3066\u3001\u306a\u3051\u308c\u3070create\u306e\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h2>create_or_find_by(Rails6\u304b\u3089\uff09<\/h2>\n<p>\u300cfind_or_create_by\u300d\u3067\u9069\u5207\u306a\u5236\u7d04\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306b\u91cd\u8907\u30a8\u30e9\u30fc\uff08ActiveRecord::RecordNotUnique\uff09\u304c\u5e38\u306b\u767a\u751f\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u305d\u306e\u5bfe\u7b56\u3068\u3057\u3066\u51fa\u3066\u304d\u305f\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<p>\u65b0\u898f\u4f5c\u6210(create)\u306etransaction\u306b\u5931\u6557(ActiveRecord::RecordNotUnique)\u3057\u305f\u3089find_by\u3057\u307e\u3059\u3002create\u304c\u5148\u306b\u5b9f\u884c\u3055\u308c\u308b\u305f\u30812\u3064\u540c\u3058\u30c7\u30fc\u30bf\u304c\u4f5c\u6210\u3055\u308c\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u6ce8\u610f\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u300cfind_or_initialize_by\u300d\u3084\u300cfind_or_create_by\u300d\u3068\u306f\uff1f Rails4\u4ee5\u4e0a\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u3063\u305fActiveRecord\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002 find_or_initialize_by \u65b0\u898f [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13089"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13089"}],"version-history":[{"count":7,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13089\/revisions"}],"predecessor-version":[{"id":20488,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13089\/revisions\/20488"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13089"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}