{"id":13085,"date":"2025-08-04T07:30:32","date_gmt":"2025-08-04T07:30:32","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13085"},"modified":"2025-08-04T07:30:32","modified_gmt":"2025-08-04T07:30:32","slug":"add-grandparent-form-field-in-rails-simpleform","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/add-grandparent-form-field-in-rails-simpleform","title":{"rendered":"How to Provide a Form Field for a Grandparent in Ruby-on-Rails SimpleForm?"},"content":{"rendered":"<h3>Using Delegation and Nested Attributes Approaches<\/h3>\n<p>In Rails, modeling indirect associations is easy, but editing them through forms can be tricky. Let&#8217;s walk through two clean ways to let users select a Category (grandparent) for a Url (child), where the association runs through an intermediate <strong>Domain <\/strong>(parent).<\/p>\n<p>We&#8217;re working with the following model setup:<\/p>\n<ul>\n<li>Category has many Domains<\/li>\n<li>Domain has many Urls and belongs to a Category<\/li>\n<li>Url belongs to a Domain<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass Category &lt; ApplicationRecord\r\n  has_many :domains\r\nend\r\n\r\nclass Domain &lt; ApplicationRecord\r\n  belongs_to :category\r\n  has_many :urls\r\nend\r\n\r\nclass Url &lt; ApplicationRecord\r\n  belongs_to :domain\r\n  has_one :category, through: :domain\r\nend\r\n<\/pre>\n<h2>Our Goal<\/h2>\n<p>In the Url form, allow the user to select a <strong>Category<\/strong>, and update the associated<strong> Domain\u2019s category_id <\/strong>accordingly.<\/p>\n<h2>Solution 1: Delegate category_id to Domain<\/h2>\n<h3>Step 1: Delegate in Url<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass Url < ApplicationRecord\r\n  belongs_to :domain\r\n  has_one :category, through: :domain\r\n\r\n  delegate :category_id, :category_id=, to: :domain, allow_nil: true\r\nend\r\n<\/pre>\n<h3>Step 2: Build domain if missing (in controller)<br \/>\n<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\ndef new\r\n  @url = Url.new\r\n  @url.build_domain unless @url.domain\r\nend\r\n<\/pre>\n<h3>Step 3: Update the form<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n<%= simple_form_for @url do |f| %>\r\n  <%= f.input :name %>\r\n  <%= f.input :category_id, collection: Category.all, label: \"Category\" %>\r\n  <%= f.button :submit %>\r\n<% end %>\r\n<\/pre>\n<h3>Step 4: Permit category_id in the controller<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nparams.require(:url).permit(:name, :category_id)\r\n<\/pre>\n<h2>Solution 2: Use Nested Attributes for Domain<\/h2>\n<h3>Step 1: Accept nested attributes in Url<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass Url < ApplicationRecord\r\n  belongs_to :domain\r\n  accepts_nested_attributes_for :domain\r\nend\r\n<\/pre>\n<h3>Step 2: Form with nested fields<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n<%= simple_form_for @url do |f| %>\r\n  <%= f.input :name %>\r\n\r\n  <%= f.simple_fields_for :domain do |df| %>\r\n    <%= df.input :category_id, collection: Category.all, label: \"Category\" %>\r\n  <% end %>\r\n\r\n  <%= f.button :submit %>\r\n<% end %>\r\n<\/pre>\n<h3>Step 3: Permit nested params in controller<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nparams.require(:url).permit(:name, domain_attributes: [:category_id])\r\n<\/pre>\n<h3>Step 4: Ensure domain is built if missing<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\ndef new\r\n  @url = Url.new\r\n  @url.build_domain\r\nend\r\n<\/pre>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/using-rails-active-storage\" target=\"_blank\">Ruby on Rails Active Storage<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Using Delegation and Nested Attributes Approaches In Rails, modeling indirect associations is easy, but editing them through forms can be tricky. Let&#8217;s walk through two clean ways to let users select a Category (grandparent) for a Url (child), where the association runs through an intermediate Domain (parent). We&#8217;re working with the following model setup: Category [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-13085","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\/13085"}],"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=13085"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13085\/revisions"}],"predecessor-version":[{"id":13087,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13085\/revisions\/13087"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13086"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}