{"id":8011,"date":"2015-11-20T05:43:57","date_gmt":"2015-11-20T05:43:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/20\/rails-create-or-update-magic-open-source-projects-minimac-magic\/"},"modified":"2022-08-30T15:03:04","modified_gmt":"2022-08-30T15:03:04","slug":"rails-create-or-update-magic-open-source-projects-minimac-magic","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/20\/rails-create-or-update-magic-open-source-projects-minimac-magic\/","title":{"rendered":"Rails create or update magic?-open source projects miniMAC\/magic"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/eCF3a.jpg?s=128&amp;g=1\" \/> <strong>Mohamad<\/strong><\/p>\n<p>You can use: <code>find_or_initialize<\/code> or <code>find_or_create<\/code><\/p>\n<p>For example, in Rails 4 syntax:<\/p>\n<pre><code>user = User.find_or_initialize_by(name: \"Roger\")\nuser.update(name: \"Elmer\")\n<\/code><\/pre>\n<p>Alternatively:<\/p>\n<pre><code>user = User.where(name: \"Roger\").first_or_initialize\n<\/code><\/pre>\n<p>Rails 3:<\/p>\n<pre><code>user = User.find_or_initialize_by_name(\"Roger\")\nuser.update_attributes(name: \"Elmer\")\n<\/code><\/pre>\n<p>If the record is newly initialised <code>update<\/code> or <code>update_attributes<\/code> will attempt to save it.<\/p>\n<p>Both forms accept a block:<\/p>\n<pre><code>User.find_or_initialize_by(name: \"Roger\") do |user|\n  user.name = \"Elmer\"\n  user.save\nend\n\nUser.where(name: \"Roger\").first_or_initialize do |user|\n  user.name = \"Elmer\"\n  user.save\nend\n<\/code><\/pre>\n<p>Documentation can be found on api.rubyonrails.org.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mohamad You can use: find_or_initialize or find_or_create For example, in Rails 4 syntax: user = User.find_or_initialize_by(name: &#8220;Roger&#8221;) user.update(name: &#8220;Elmer&#8221;) Alternatively: user = User.where(name: &#8220;Roger&#8221;).first_or_initialize Rails 3: user = User.find_or_initialize_by_name(&#8220;Roger&#8221;) user.update_attributes(name: &#8220;Elmer&#8221;) If the record is newly initialised update or update_attributes will attempt to save it. Both forms accept a block: User.find_or_initialize_by(name: &#8220;Roger&#8221;) do |user| user.name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8011","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8011"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8011\/revisions"}],"predecessor-version":[{"id":8699,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8011\/revisions\/8699"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}