{"id":10948,"date":"2024-07-19T07:05:12","date_gmt":"2024-07-19T07:05:12","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10948"},"modified":"2024-07-19T12:02:23","modified_gmt":"2024-07-19T12:02:23","slug":"ruby-on-rails-build-method","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/ruby-on-rails-build-method","title":{"rendered":"Build Method on Ruby on Rails"},"content":{"rendered":"<h2>What is the .build method?<\/h2>\n<p>In Ruby on Rails, the .build method is used within the context of Active Record associations to create a new associated object in memory. It&#8217;s particularly helpful when working with nested forms in your Rails application.<\/p>\n<p>In simple words, the build method is a way to create a new item (or record) that is connected to another item without immediately saving it to the database.<\/p>\n<h3>Key Points<\/h3>\n<ul>\n<li>.build creates a new object in memory, but it doesn&#8217;t save it to the database.<\/li>\n<li>It&#8217;s often used with forms to pre-populate associated fields and simplify form submission.<\/li>\n<li>The new object is linked to the parent object through the foreign key, but the association isn&#8217;t persisted until you explicitly save both objects.<\/li>\n<\/ul>\n<h3>Example Scenario<\/h3>\n<p>There is a blog website. On this website:<br \/>\n-&gt; We have Users who write Articles.<br \/>\nIn our code, a User can have many Articles.<\/p>\n<h2>How build Works<\/h2>\n<p><strong>1. Find the User:<\/strong> We will first find the user who will write the article.<br \/>\n<strong>2. Create the Article:<\/strong> We will use the build method to create a new article for that user. This article has all the information it needs to be connected to that user, but it&#8217;s not saved to the database yet.<br \/>\n<strong>3. Save the Article:<\/strong> When we&#8217;re ready, we will save the article to the database.<\/p>\n<h2>Why Use build?<\/h2>\n<ul>\n<li>Associations: It automatically connects the new article to the user.<\/li>\n<li>Delay Saving: We can set up everything we need before actually saving it to the database.<\/li>\n<\/ul>\n<h2>How to Use build<\/h2>\n<h3>In the Rails Console-<\/h3>\n<p><strong>1. Find the User:<\/strong><br \/>\nuser = User.find(1)<\/p>\n<p><strong>2. Build the Article:<\/strong><br \/>\narticle = user.articles.build(title: &#8220;New Article&#8221;, content: &#8220;This is my first Article&#8221;)<\/p>\n<ul>\n<li>user.articles.build creates a new article connected to user.<\/li>\n<li>The article is not saved yet.<\/li>\n<\/ul>\n<p><strong>3. Save the Article:<\/strong><br \/>\narticle.save<\/p>\n<h3>In the Controller-<\/h3>\n<p>When we have a form on our website to create a new article, we can use build in the controller to handle this:<\/p>\n<p><strong>1. New Action:<\/strong> Set up the form with a new article connected to a user.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">def new\r\n@user = User.find(params[:user_id])\r\n@article = @user.articles.build\r\nend<\/pre>\n<p><strong>2. Create Action:<\/strong> Handle form submission and save the article.<br \/>\nWe will create a create method to handle the submission<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">def create \r\n@user = User.find(params[:user_id]) \r\n    @article = @user.articles.build(article_params) \r\nif @article.save \r\nredirect_to @article, notice: 'Article was successfully created.' \r\nelse\r\nrender :new end \r\nend\r\nend\r\n<\/pre>\n<h2>Summary<\/h2>\n<p>By effectively using the .build method, we can streamline the creation of associated objects in our Rails applications and provide a seamless user experience for nested forms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the .build method? In Ruby on Rails, the .build method is used within the context of Active Record associations to create a new associated object in memory. It&#8217;s particularly helpful when working with nested forms in your Rails application. In simple words, the build method is a way to create a new item [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10949,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-10948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10948"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10948"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10948\/revisions"}],"predecessor-version":[{"id":10965,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10948\/revisions\/10965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10949"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}