<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by JDev Blogs on Medium]]></title>
        <description><![CDATA[Stories by JDev Blogs on Medium]]></description>
        <link>https://medium.com/@jaskaranahuja111?source=rss-6ab12eeacf79------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*93jzgUEPIhBwccaYus6CoA.png</url>
            <title>Stories by JDev Blogs on Medium</title>
            <link>https://medium.com/@jaskaranahuja111?source=rss-6ab12eeacf79------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 09:05:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jaskaranahuja111/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ Mastering Nested Resources in Rails: A Complete Guide to User-Post Management]]></title>
            <link>https://medium.com/@jaskaranahuja111/mastering-nested-resources-in-rails-a-complete-guide-to-user-post-management-69c9a44a0660?source=rss-6ab12eeacf79------2</link>
            <guid isPermaLink="false">https://medium.com/p/69c9a44a0660</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[nested-attributes]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[JDev Blogs]]></dc:creator>
            <pubDate>Tue, 24 Dec 2024 07:31:46 GMT</pubDate>
            <atom:updated>2024-12-24T07:31:46.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_FzAgdLxrV4bQGSmb8EMTg.jpeg" /></figure><p>Hey there, guys! Have you ever struggled to manage relationships between models while keeping your code neat and your app functional? Well, worry no more! Today, we’re diving into the wonderful world of nested resources, and I’ll walk you through building a simple yet powerful User-Post system. Let’s make coding fun and approachable!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*Z7_d7dYnLYSrhYVQecZY_A.png" /></figure><p>routes.rb: Defining Routes</p><pre>root to: &quot;users#index&quot;<br>resources :users do<br>  resources :posts<br>end</pre><p><strong>Relationship with Posts</strong></p><ul><li>The line has_many :posts establishes that a user can have multiple posts.</li><li>Adding dependent: :destroy ensures that when a user is deleted, all their posts are deleted too. This keeps your database tidy—no orphaned posts hanging around!</li></ul><p><strong>Nested Attributes for Posts</strong></p><ul><li>accepts_nested_attributes_for :posts allows us to manage posts directly through the user form.</li><li>Adding allow_destroy: true lets users delete specific posts right from the form.</li></ul><p>user.rb: User Model</p><pre>class User &lt; ApplicationRecord<br>  has_many :posts , dependent: :destroy<br>  accepts_nested_attributes_for :posts , allow_destroy: true<br>end</pre><p>user.rb: User Model</p><pre>class Post &lt; ApplicationRecord<br>  belongs_to :user<br>end</pre><p>Let’s start with how users will interact with our app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/224/1*aC2BT5JRm1mxP6ZBdz3ouQ.png" /></figure><p>The _form.html.erb file is where the magic happens. This is the heart of our app’s user interface—where we collect details about a user and optionally manage their posts in the same form. Sounds fancy, but it’s actually super intuitive!</p><p>_form.html.erb: The Backbone of User Creation/Editing</p><pre>&lt;%= form_with(model: @user, local: true, html: { id: &#39;user-form&#39; }) do |user| %&gt;<br>  &lt;h2&gt;Your Details&lt;/h2&gt;<br>  &lt;p&gt;<br>    &lt;%= user.label :name %&gt;<br>    &lt;%= user.text_field :name %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;%= user.label :job %&gt;<br>    &lt;%= user.text_field :job %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;%= user.label :dob %&gt;<br>    &lt;%= user.date_field :dob %&gt;<br>  &lt;/p&gt;<br>&lt;button type=&quot;button&quot; id=&quot;toggle-posts&quot;&gt;Add Posts&lt;/button&gt;<br>  &lt;div id=&quot;posts-section&quot; style=&quot;display: none;&quot;&gt;<br>    &lt;h2&gt;Posts&lt;/h2&gt;<br>    &lt;%= user.fields_for :posts do |post| %&gt;<br>      &lt;div class=&quot;post-fields&quot;&gt;<br>        &lt;p&gt;<br>          &lt;%= post.label :title %&gt;<br>          &lt;%= post.text_field :title, disabled: true %&gt;<br>        &lt;/p&gt;<br>        &lt;p&gt;<br>          &lt;%= post.label :body %&gt;&lt;br&gt;<br>          &lt;%= post.text_area :body, disabled: true %&gt;<br>        &lt;/p&gt;<br>      &lt;/div&gt;<br>    &lt;% end %&gt;<br>  &lt;/div&gt;<br>  &lt;%= user.hidden_field :include_posts, value: false %&gt;<br>  &lt;%= user.submit &#39;Submit&#39; %&gt;<br>&lt;% end %&gt;<br>&lt;script&gt;<br>    document.addEventListener(&#39;DOMContentLoaded&#39;, function() {<br>        const form = document.getElementById(&#39;user-form&#39;);<br>        const toggleButton = document.getElementById(&#39;toggle-posts&#39;);<br>        const postsSection = document.getElementById(&#39;posts-section&#39;);<br>        const postFields = form.querySelectorAll(&#39;.post-fields input, .post-fields textarea&#39;);<br>        const includePostsField = form.querySelector(&#39;[name*=&quot;include_posts&quot;]&#39;);<br>        let postsEnabled = false;<br>        toggleButton.addEventListener(&#39;click&#39;, function() {<br>            postsEnabled = !postsEnabled;<br>            // Toggle visibility and button text<br>            postsSection.style.display = postsEnabled ? &#39;block&#39; : &#39;none&#39;;<br>            toggleButton.textContent = postsEnabled ? &#39;Remove Posts&#39; : &#39;Add Posts&#39;;<br>            // Toggle fields<br>            postFields.forEach(field =&gt; {<br>                field.disabled = !postsEnabled;<br>            });<br>            // Update hidden field<br>            includePostsField.value = postsEnabled;<br>        });<br>        // Handle form submission<br>        form.addEventListener(&#39;submit&#39;, function(e) {<br>            if (!postsEnabled) {<br>                // Clear post field values when not including posts<br>                postFields.forEach(field =&gt; {<br>                    field.value = &#39;&#39;;<br>                });<br>            }<br>        });<br>    });<br>&lt;/script&gt;</pre><h4>Listing Users (index.html.erb)</h4><p>This page is like a directory of users in your system. It shows their names, jobs, and gives you action buttons to show, edit, or delete them. If there aren’t any users yet, don’t worry — the app politely tells you, “No Posts available.”</p><p>Oh, and there’s a shiny New User button to add someone to the list. Easy, right?</p><p>index.html.erb: Listing Users</p><pre>&lt;%=link_to &#39;New User&#39;, new_user_path %&gt;<br>&lt;% if @users.empty? %&gt;<br>  &lt;h1&gt;No Posts available&lt;/h1&gt;<br>&lt;%else %&gt;<br>&lt;table&gt;<br>  &lt;thead&gt;<br>  &lt;tr&gt;<br>    &lt;th&gt;Full Name&lt;/th&gt;<br>    &lt;th&gt;Job&lt;/th&gt;<br>    &lt;th colspan=&quot;3&quot;&gt;Actions&lt;/th&gt;<br>  &lt;/tr&gt;<br>  &lt;/thead&gt;<br>  &lt;tbody&gt;<br>  &lt;%@users.each do |user| %&gt;<br>  &lt;tr&gt;<br>    &lt;td&gt;&lt;%= user.name %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%=user.job %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%= link_to &#39;Show&#39;,user_path(user) %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%=link_to &#39;Edit&#39;,edit_user_path(user) %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%=button_to &#39;Delete&#39;,user_path(user) , method: :delete %&gt;&lt;/td&gt;<br>  &lt;/tr&gt;<br>    &lt;% end %&gt;<br>  &lt;/tbody&gt;<br>&lt;/table&gt;<br>&lt;%end %&gt;</pre><h4>Viewing User Details (show.html.erb)</h4><p>Clicking “Show” opens the user’s profile page. Here, you’ll see the user’s name, job, and date of birth. If they’ve written posts, those will be displayed too, showing the title and body. Want to see more posts? There’s a button for that!</p><p><strong>Users View: Dynamic Form for Users and Posts</strong></p><p>show.html.erb: Viewing a User&#39;s Details</p><pre>&lt;p&gt;<br>  &lt;strong&gt;Name:&lt;/strong&gt;<br>  &lt;%= @user.name %&gt;<br>&lt;/p&gt;<br>&lt;p&gt;<br>  &lt;strong&gt;Date:&lt;/strong&gt;<br>  &lt;%= @user.dob %&gt;<br>&lt;/p&gt;<br>&lt;p&gt;<br>  &lt;strong&gt;Job:&lt;/strong&gt;<br>  &lt;%= @user.job %&gt;<br>&lt;/p&gt;<br>&lt;p&gt;<br>&lt;%if @post.present? %&gt;<br>  &lt;% @posts.each do |post| %&gt;<br>    &lt;p&gt;<br>      &lt;strong&gt;Title:&lt;/strong&gt;<br>      &lt;%= post.title %&gt;<br>    &lt;/p&gt;<br>    &lt;p&gt;<br>      &lt;strong&gt;Body:&lt;/strong&gt;<br>      &lt;%= post.body %&gt;<br>    &lt;/p&gt;<br>  &lt;%end %&gt;<br>  &lt;%end %&gt;<br>  &lt;%= link_to &#39;Posts&#39;, user_posts_path(@user)%&gt;</pre><p><strong>Posts View</strong></p><p>index.html.erb: Listing Posts</p><pre>&lt;%=link_to &#39;User&#39;, new_user_path %&gt;<br>&lt;% if @posts.empty? %&gt;<br>  &lt;h1&gt;No Posts available&lt;/h1&gt;<br>  &lt;%else %&gt;<br>  &lt;table&gt;<br>    &lt;thead&gt;<br>    &lt;tr&gt;<br>      &lt;th&gt;Full Name&lt;/th&gt;<br>      &lt;th&gt;Post&lt;/th&gt;<br>      &lt;th colspan=&quot;3&quot;&gt;Actions&lt;/th&gt;<br>    &lt;/tr&gt;<br>    &lt;/thead&gt;<br>    &lt;tbody&gt;<br>    &lt;%@posts.each do |post| %&gt;<br>      &lt;tr&gt;<br>        &lt;td&gt;&lt;%= post.user.name %&gt;&lt;/td&gt;<br>        &lt;td&gt;&lt;%=post.title %&gt;&lt;/td&gt;<br>        &lt;td&gt;&lt;%= link_to &#39;Show&#39;,user_post_path(post.user,post) %&gt;&lt;/td&gt;<br>        &lt;td&gt;&lt;%=link_to &#39;Edit&#39;,edit_user_path(post.user,post) %&gt;&lt;/td&gt;<br>        &lt;td&gt;&lt;%=button_to &#39;Delete&#39;,user_post_path(post.user,post) , method: :delete %&gt;&lt;/td&gt;<br>      &lt;/tr&gt;<br>    &lt;% end %&gt;<br>    &lt;/tbody&gt;<br>  &lt;/table&gt;<br>&lt;% end %&gt;<br>&lt;%= link_to &#39;Back to Users &#39;,users_path  %&gt;</pre><p>show.html.erb: Viewing a Post&#39;s Details</p><pre>&lt;p&gt;<br>  &lt;strong&gt;Name:&lt;/strong&gt;<br>  &lt;%= @user.name %&gt;<br>&lt;/p&gt;<br>&lt;p&gt;<br>  &lt;strong&gt;Title:&lt;/strong&gt;<br>  &lt;%= @post.title %&gt;<br>&lt;/p&gt;<br>&lt;p&gt;<br>  &lt;strong&gt;Body:&lt;/strong&gt;<br>  &lt;%= @post.body %&gt;<br>&lt;/p&gt;<br>&lt;%= link_to &#39;Back to Posts&#39;, user_posts_path(@user)%&gt;<br>&lt;%= link_to &#39;Back to Users &#39;,users_path  %&gt;</pre><h3>Controllers</h3><h4>UsersController: Managing Users</h4><ul><li>Handles CRUD operations for users.</li><li>Includes posts_attributes in user_params to handle nested post data.</li></ul><pre>class UsersController &lt; ApplicationController<br>  before_action :set_user, only: [:show, :edit, :update, :destroy]<br>def index<br>    @users = User.all<br>  end<br>  def show<br>  end<br>  def new<br>    @user = User.new<br>    @user.posts.build<br>  end<br>  def create<br>    @user = User.new(user_params)<br>    if @user.save<br>      redirect_to user_path(@user)<br>    else<br>      render :new<br>    end<br>  end<br>  def edit<br>    @user.posts<br>  end<br>  def update<br>    if @user.update(user_params)<br>      redirect_to user_path(@user)<br>    else<br>      render :edit<br>    end<br>  end<br>  def destroy<br>    @user.destroy<br>    redirect_to users_path<br>  end<br>  private<br>  def set_user<br>    @user = User.find(params[:id])<br>  end<br>  def user_params<br>    params.require(:user).permit(:name, :job, :dob, posts_attributes: [:id, :title, :body, :_destroy])<br>  end<br>end</pre><h4>PostsController: Managing Posts</h4><ul><li>Nested within users to ensure posts are scoped to a specific user.</li><li>set_user retrieves the parent user before performing any post action.</li></ul><pre>class PostsController &lt; ApplicationController<br>  before_action :set_user<br>  before_action :set_post, only: [:show, :edit, :update, :destroy]<br>  def index<br>    @posts = @user.posts<br>  end<br>def show<br>  end<br>def new<br>    @post = @user.posts.build<br>  end<br>  def create<br>    @post = @user.posts.build(post_params)<br>    if @post.save<br>      redirect_to user_posts_path(@user)<br>    else<br>      render :new<br>    end<br>  end<br>  def edit<br>  end<br>  def update<br>    if @post.update(post_params)<br>      redirect_to user_posts_path(@user)<br>    else<br>      render :edit<br>    end<br>  end<br>  def destroy<br>    @post.destroy<br>    redirect_to user_posts_path(@user)<br>  end<br>  private<br>  def set_user<br>    @user = User.find(params[:user_id])<br>  end<br>  def set_post<br>    @post = @user.posts.find(params[:id])<br>  end<br>  def post_params<br>    params.require(:post).permit(:title, :body)<br>  end<br>end</pre><h3>A Few Highlights to Make You Smile</h3><ol><li><strong>Nested Resources Made Simple</strong><br>Every post belongs to a user, and our routes, controllers, and views reflect that relationship. Clean, logical, and easy to follow!</li><li><strong>Smart Views</strong><br>No users? No posts? No problem! The app gracefully handles empty states, giving users a clear message while keeping things user-friendly.</li><li><strong>One-Stop User Management</strong><br>Thanks to accepts_nested_attributes_for, we can manage users and their posts from the same form. Less clicking, more doing!</li><li><strong>Reusable Code</strong><br>Methods like set_user and set_post make our controllers tidy and easy to read. No unnecessary clutter here.</li></ol><p>This is just the start of what you can do with nested resources in Rails. Whether you’re building a blog, a marketplace, or something completely unique, these concepts will serve you well.</p><p>Got questions or ideas? Let’s chat in the comments below — I’d love to hear how you’re using nested resources in your projects and like and follow for more so, that I keep posting this keeps me motivated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69c9a44a0660" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby on Rails Associations Made Simple: A Developer’s Guide to has_many Relationships]]></title>
            <link>https://medium.com/@jaskaranahuja111/ruby-on-rails-associations-made-simple-a-developers-guide-to-has-many-relationships-ba65b89899e3?source=rss-6ab12eeacf79------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba65b89899e3</guid>
            <category><![CDATA[rails-associations]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[mvc-architecture]]></category>
            <category><![CDATA[crud-operations]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[JDev Blogs]]></dc:creator>
            <pubDate>Tue, 17 Dec 2024 09:33:44 GMT</pubDate>
            <atom:updated>2024-12-17T09:33:44.629Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey there, fellow Rails enthusiasts! 🚀 Ever found yourself scratching your head about model associations in Ruby on Rails? You’re not alone! Today, we’re diving deep into the world of has_many associations - a powerful feature that&#39;ll make your database relationships sing.</p><h3>Why association matters</h3><p>Picture this: You’re building a blog platform. A user can have multiple posts, and each post can have multiple comments. Sounds complex? Not with Rails associations! These magical connections between models are like invisible threads that tie your data together seamlessly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/0*h2In60-_thuQoG6t.png" /></figure><h3>Real-World Scenarios</h3><p>Consider these classic examples:</p><ul><li>A User can have multiple Posts</li><li>A Posts can have multiple Comments</li><li>A Company can have multiple Employees</li><li>A Teacher can have multiple Students</li></ul><h3>The Anatomy of a has_many Relationship</h3><p>When you define a has_many association, you&#39;re essentially telling Rails:</p><ul><li>One model can be the parent of multiple related records</li><li>The related records can be created, accessed, and managed directly through the parent model</li><li>Rails will automatically generate helper methods to make working with these relationships seamless</li></ul><h3>Setting Up Models: The Association Magic</h3><p>Here’s how the models are defined with their relationships:</p><h3>User Model</h3><pre>class User &lt; ApplicationRecord<br>  # A user can have multiple posts<br>  has_many :posts<br>end</pre><h3>Post Model</h3><pre>class Post &lt; ApplicationRecord<br>  # Each post belongs to one user<br>  belongs_to :user<br>  # A post can have multiple comments<br>  has_many :comments<br>end</pre><h3>Comment Model</h3><pre>class Comment &lt; ApplicationRecord<br>  # Each comment belongs to one post<br>  belongs_to :post<br>end</pre><h3>User Controller</h3><p>The UsersController handles CRUD operations for the User model.</p><pre>class UsersController &lt; ApplicationController<br>  # Display all users<br>  def index<br>    @users = User.all<br>  end<br># Display details of a specific user<br>  def show<br>    @user = User.find(params[:id])<br>  end<br> # Initialize a new user object for the form<br>  def new<br>    @user = User.new<br>  end<br># Create a new user<br>  def create<br>    @user = User.new(user_params)<br>    if @user.save<br>      # Redirect to the user&#39;s show page if saved successfully<br>      redirect_to @user<br>    else<br>      # Redirect to the new user form if validation fails<br>      redirect_to &#39;new&#39;<br>    end<br>  end<br>  # Fetch the user object for editing<br>  def edit<br>    @user = User.find(params[:id])<br>  end<br>  # Update an existing user<br>  def update<br>    @user = User.find(params[:id])<br>    if @user.update(user_params)<br>      # Redirect to the user&#39;s show page on success<br>      redirect_to @user<br>    else<br>      # Redirect back to the edit form if update fails<br>      redirect_to &#39;edit&#39;<br>    end<br>  end<br>  # Delete a user<br>  def destroy<br>    @user = User.find(params[:id])<br>    @user.destroy<br>    # Redirect to the users index after deletion<br>    redirect_to users_path<br>  end<br>  private<br>  # Strong parameters for security<br>  def user_params<br>    params.require(:user).permit(:firstname, :lastname, :dob, :job)<br>  end<br>end</pre><h3>Posts Controller: Nested Resources in Action</h3><p>The PostsController is nested within the UsersController, allowing posts to be created and managed for a specific user.</p><pre>class PostsController &lt; ApplicationController<br>  # Display all posts<br>  def index<br>    @posts = Post.all<br>  end</pre><pre>  # Show details of a specific post<br>  def show<br>    @user = User.find(params[:user_id]) # Find the parent user<br>    @post = Post.find(params[:id])     # Find the post<br>  end</pre><pre>  # Initialize a new post object for the form<br>  def new<br>    @user = User.find(params[:user_id])<br>    @post = Post.new<br>  end</pre><pre>  # Create a new post for a specific user<br>  def create<br>    @user = User.find(params[:user_id])<br>    @post = @user.posts.new(post_params) # Associate the post with the user<br>    if @post.save<br>      # Redirect to the post&#39;s show page on success<br>      redirect_to user_post_path(@user, @post)<br>    else<br>      # Redirect to the new post form if validation fails<br>      redirect_to &#39;new&#39;<br>    end<br>  end</pre><pre>  # Fetch the post object for editing<br>  def edit<br>    @user = User.find(params[:user_id])<br>    @post = @user.posts.find(params[:id])<br>  end</pre><pre>  # Update a specific post<br>  def update<br>    @user = User.find(params[:user_id])<br>    @post = @user.posts.find(params[:id])<br>    if @post.update(post_params)<br>      # Redirect to the posts index for the user<br>      redirect_to user_posts_path<br>    else<br>      # Redirect to the edit form if update fails<br>      redirect_to &#39;edit&#39;<br>    end<br>  end</pre><pre>  # Delete a specific post<br>  def destroy<br>    @user = User.find(params[:user_id])<br>    @post = @user.posts.find(params[:id])<br>    @post.destroy<br>    # Redirect to the user&#39;s posts index after deletion<br>    redirect_to user_posts_path<br>  end</pre><pre>  private</pre><pre>  # Strong parameters for security<br>  def post_params<br>    params.require(:post).permit(:title, :description)<br>  end<br>endViews: Bringing It All Together</pre><h3>User Form Partial (_form.html.erb):</h3><pre>&lt;%= form_with model: @user, local: true do |user| %&gt;<br>  &lt;p&gt;<br>    &lt;strong&gt;&lt;%=user.label :firstname %&gt;&lt;/strong&gt;<br>    &lt;%=user.text_field :firstname %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;strong&gt;&lt;%=user.label :lastname %&gt;&lt;/strong&gt;<br>    &lt;%= user.text_field :lastname %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;strong&gt;&lt;%=user.label :dob %&gt;&lt;/strong&gt;<br>    &lt;%= user.date_field :dob %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;strong&gt;&lt;%=user.label :job %&gt;&lt;/strong&gt;<br>    &lt;%= user.text_field :job %&gt;<br>  &lt;/p&gt;<br>  &lt;%= user.submit %&gt;<br>&lt;% end %&gt;<br>&lt;%= link_to &#39;Back To Users&#39; , users_path %&gt;</pre><h3>Post Form (new.html.erb):</h3><pre>&lt;h1&gt;New Post&lt;/h1&gt;<br>&lt;%= form_with(model: [@user, @post], local: true) do |post| %&gt;<br>  &lt;p&gt;<br>    &lt;%= post.label :title %&gt;<br>    &lt;%= post.text_field :title %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;%=post.label :description %&gt;<br>    &lt;%=post.text_field :description %&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    &lt;%= post.submit %&gt;<br>  &lt;/p&gt;<br>&lt;% end %&gt;</pre><h3>Posts Index View:</h3><pre>&lt;h1&gt;All Posts&lt;/h1&gt;<br>&lt;table&gt;<br>  &lt;tr&gt;<br>    &lt;th&gt;Title&lt;/th&gt;<br>    &lt;th&gt;Post&lt;/th&gt;<br>    &lt;th&gt;Users&lt;/th&gt;<br>    &lt;th colspan=&quot;3&quot;&gt;Actions&lt;/th&gt;<br>  &lt;/tr&gt;<br>  &lt;% @posts.each do |post| %&gt;<br>  &lt;tr&gt;<br>    &lt;td&gt;&lt;%= post.title %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%= post.description %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%= post.user.firstname %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%=link_to &#39;Show&#39;, user_post_path(post.user_id, post.id) %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%=link_to &#39;Edit&#39;, edit_user_post_path(post.user_id, post.id) %&gt;&lt;/td&gt;<br>    &lt;td&gt;&lt;%=button_to &#39;Delete&#39;, user_post_path(post.user_id, post.id), method: :delete %&gt;&lt;/td&gt;<br>  &lt;/tr&gt;<br>  &lt;% end %&gt;<br>&lt;/table&gt;<br>&lt;%= link_to &#39;Back to users&#39;, users_path %&gt;</pre><h3>Pro Tips for Association Mastery</h3><ol><li><strong>Always Use </strong><strong>.build or </strong><strong>.new for Associated Models</strong> When creating associated records, use methods like user.posts.build or user.posts.new.</li><li><strong>Leverage Nested Routes</strong> Set up nested routes to create intuitive URLs like /users/1/posts.</li><li><strong>Don’t Forget Validations</strong> While associations are powerful, always add validations to maintain data integrity.</li></ol><h3>Wrapping Up</h3><p>Associations in Rails aren’t just technical — they’re about creating meaningful, interconnected data relationships. They transform your models from isolated entities to a living, breathing ecosystem.</p><p>Want to level up your Rails game? Practice, experiment, and most importantly, have fun building! 🎉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba65b89899e3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>