{"id":806,"date":"2009-05-20T16:11:37","date_gmt":"2009-05-20T07:11:37","guid":{"rendered":"http:\/\/ecpplus.net\/weblog\/?p=806"},"modified":"2021-06-05T15:10:38","modified_gmt":"2021-06-05T06:10:38","slug":"activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86\/","title":{"rendered":"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406"},"content":{"rendered":"<p>Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048\u3070\u3001<code>User.create!(params[:user])<\/code> \u3068\u304b\u3057\u3066\u3001\u5931\u6557\u3059\u308b\u3068\u3001<code>ActiveRecord::RecordInvalid<\/code> \u3092\u6295\u3052\u3066\u304f\u308c\u308b\u306e\u3060\u3051\u3069\u3001\u30a8\u30e9\u30fc\u3092\u3069\u3046\u3084\u3063\u3066\u53d6\u308b\u306e\u304b\u8abf\u3079\u305f\u306e\u3067\u30e1\u30e2\u3002<\/p>\n<p>\u3088\u304f\u3042\u308b\u4f8b\u3067\u3001\u30e6\u30fc\u30b6\u304c\u4f5c\u308c\u305f\u3089 index \u3078\u9077\u79fb\u3057\u3066\u3001\u4f5c\u308c\u306a\u304b\u3063\u305f\u3089 new \u3067\u518d\u5165\u529b\u3055\u305b\u308b\u3068\u3044\u3046\u5834\u5408\u3002<\/p>\n<h3>create\u3092\u4f7f\u3046\u5834\u5408<\/h3>\n<pre><code class=\"language-ruby\">def create\n  if @user = User.create(params[:user])\n    flash[:notice] = &#039;\u30e6\u30fc\u30b6\u4f5c\u6210\u3057\u307e\u3057\u305f&#039;\n    redirect_to action: :index\n  else\n    # \u3053\u3053\u3067\u306f\u3001@user.errors \u3067\u30a8\u30e9\u30fc\u304c\u53d6\u308c\u308b\n    render action: :new\n  end\nend<\/code><\/pre>\n<h3>create!\u3092\u4f7f\u3046\u5834\u5408<\/h3>\n<pre><code class=\"language-ruby\">def create\n  @user = User.create!(params[:user])\n  flash[:notice] = &#039;\u30e6\u30fc\u30b6\u4f5c\u6210\u3057\u307e\u3057\u305f&#039;\n  redirect_to action: :index\nrescue ActiveRecord::RecordInvalid =&gt; e\n  # @user \u306f nil \u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001 @user.errors \u3067\u30a8\u30e9\u30fc\u304c\u53d6\u308c\u306a\u3044\u3002\n  render action: :new\nend<\/code><\/pre>\n<p>\u3068\u3057\u305f\u3068\u304d\u3001<code>create!<\/code> \u3067\u4f8b\u5916\u304c\u98db\u3093\u3067\u3044\u308b\u306e\u3067\u3001\u5f53\u7136 <code>@user<\/code> \u306f <code>nil<\/code> \u3068\u306a\u3063\u3066\u307e\u3059\u3002\u306a\u306e\u3067\u3001rescue \u53e5\u306e\u4e2d\u3067 <code>@user.errors<\/code> \u3067\u306f\u30a8\u30e9\u30fc\u304c\u53d6\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u8abf\u3079\u305f\u3068\u3053\u308d\u3001<code>e.record<\/code> \u3067\u3001ActiveRecord \u306e\u4f8b\u5916\u3092\u767a\u751f\u3055\u305b\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u53d6\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-ruby\">def create\n  @user = User.create!(params[:user])\n  flash[:notice] = &#039;\u30e6\u30fc\u30b6\u4f5c\u6210\u3057\u307e\u3057\u305f&#039;\n  redirect_to action: :index\nrescue ActiveRecord::RecordInvalid =&gt; e\n  # e.record \u306b User\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5165\u3063\u3066\u308b\u3002\n  @user = e.record\n  render action: :new\nend<\/code><\/pre>\n<p>\u305f\u3068\u3048\u3070\u3001View\u5185\u3067<\/p>\n<pre><code class=\"language-erb\">&lt;%= error_messages_for :user %&gt;<\/code><\/pre>\n<p>\u3068\u304b\u3057\u305f\u3044\u5834\u5408\u306f\u3001 <code>@user = e.record<\/code> \u3068\u3057\u3066\u304a\u3051\u3070\u30a8\u30e9\u30fc\u304c\u697d\u306b\u8868\u793a\u51fa\u6765\u3066\u826f\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048\u3070\u3001User.create!(params[:user]) \u3068\u304b\u3057\u3066\u3001\u5931\u6557\u3059\u308b\u3068\u3001ActiveRecord:: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[10],"class_list":["post-806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-rails"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048\" \/>\n\t<meta name=\"robots\" content=\"max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n\t<meta name=\"author\" content=\"\u3061\u3085\u3046\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/ecpplus.net\/weblog\/activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"ja_JP\" \/>\n\t\t<meta property=\"og:site_name\" content=\"\u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406 | \u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0\" \/>\n\t\t<meta property=\"og:description\" content=\"Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/ecpplus.net\/weblog\/activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/ecpplus.net\/weblog\/wp-content\/uploads\/2011\/08\/ruby_on_rails.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/ecpplus.net\/weblog\/wp-content\/uploads\/2011\/08\/ruby_on_rails.png\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t\t<meta property=\"article:tag\" content=\"development\" \/>\n\t\t<meta property=\"article:tag\" content=\"rails\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2009-05-20T07:11:37+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2021-06-05T06:10:38+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@ecpplus\" \/>\n\t\t<meta name=\"twitter:title\" content=\"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406 | \u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@ecpplus\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/ecpplus.net\/weblog\/wp-content\/uploads\/2011\/08\/ruby_on_rails.png\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406 | \u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0","description":"Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048","canonical_url":"https:\/\/ecpplus.net\/weblog\/activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86\/","robots":"max-snippet:-1, max-image-preview:large, max-video-preview:-1","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"ja_JP","og:site_name":"\u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0","og:type":"article","og:title":"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406 | \u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0","og:description":"Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048","og:url":"https:\/\/ecpplus.net\/weblog\/activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86\/","og:image":"https:\/\/ecpplus.net\/weblog\/wp-content\/uploads\/2011\/08\/ruby_on_rails.png","og:image:secure_url":"https:\/\/ecpplus.net\/weblog\/wp-content\/uploads\/2011\/08\/ruby_on_rails.png","og:image:width":1920,"og:image:height":768,"article:tag":["development","rails"],"article:published_time":"2009-05-20T07:11:37+00:00","article:modified_time":"2021-06-05T06:10:38+00:00","twitter:card":"summary_large_image","twitter:site":"@ecpplus","twitter:title":"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406 | \u5909\u306a\u30e4\u30d0\u3044\u3082\u3093\u30ed\u30b0","twitter:description":"Rails \u306e ActiveRecord \u3067\u3001save! \u3068\u304b\u3059\u308b\u3068\u4fdd\u5b58\u306b\u5931\u6557\u3057\u305f\u3068\u304d\u4f8b\u5916\u3092\u6295\u3052\u3066\u304f\u308c\u308b\u3002\u4f8b\u3048","twitter:creator":"@ecpplus","twitter:image":"https:\/\/ecpplus.net\/weblog\/wp-content\/uploads\/2011\/08\/ruby_on_rails.png"},"aioseo_meta_data":{"post_id":"806","title":null,"description":null,"keywords":[],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"article","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary_large_image","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":{"locations":{"business":{"name":"","businessType":"","image":"","areaServed":"","urls":{"website":"","aboutPage":"","contactPage":""},"address":{"streetLine1":"","streetLine2":"","zipCode":"","city":"","state":"","country":"","addressFormat":"#streetLineOne\n#streetLineTwo\n#city, #state #zipCode"},"contact":{"email":"","phone":"","phoneFormatted":"","fax":"","faxFormatted":""},"ids":{"vat":"","tax":"","chamberOfCommerce":""},"payment":{"priceRange":"","currenciesAccepted":"","methods":""}}},"openingHours":{"useDefaults":true,"show":true,"alwaysOpen":false,"use24hFormat":false,"timezone":"","labels":{"closed":"","alwaysOpen":""},"days":{"monday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"tuesday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"wednesday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"thursday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"friday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"saturday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"sunday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"}}}},"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-01-02 06:30:54","updated":"2022-09-29 08:44:40","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/ecpplus.net\/weblog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/ecpplus.net\/weblog\/category\/development\/\" title=\"Development\">Development<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tActiveRecord \u306e\u4f8b\u5916\u51e6\u7406\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/ecpplus.net\/weblog"},{"label":"Development","link":"https:\/\/ecpplus.net\/weblog\/category\/development\/"},{"label":"ActiveRecord \u306e\u4f8b\u5916\u51e6\u7406","link":"https:\/\/ecpplus.net\/weblog\/activerecord-%e3%81%ae%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86\/"}],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=806"}],"version-history":[{"count":4,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/806\/revisions"}],"predecessor-version":[{"id":3234,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/806\/revisions\/3234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}