title: Loop through has many through relationship in Rails
body: <p>I have a Post model and then a Reference model that serves as a model for the relationship between two Posts.  So a single Post (say, POST A) will have some 'referred' posts (POST B, POST C, etc) that it referred to... and it will also have some 'referring' posts (POST D, POST E, etc) that referred to it. From POST A's 'show' view, I am simply trying to loop through and show all 'referred' Posts and all 'referring' posts above and below it.  I want to use the reference relationships model to do this. The references &amp; referred_relationships/referring_relationships are created successfully when a post is created, but I cannot figure out the correct code to place in the view to show these related post titles in a list...any help would be appreciated on what the for each loop mentioned below should look like:</p>
  
  <p>Reference.rb</p>
  
  <pre><code>class Reference &lt; ApplicationRecord 
    belongs_to :referred, class_name: "Post"
    belongs_to :referring, class_name: "Post"
    validates :referred_id, presence: true
    validates :referring_id, presence: true
  end
  </code></pre>
  
  <p>Post.rb</p>
  
  <pre><code>class Post &lt; ApplicationRecord
  
    has_many :referred_relationships, foreign_key: "referred_id", 
                                    class_name:  "Reference",
                                    dependent:   :destroy
    has_many :referred, through: :referred_relationships, source: :referred
  
    has_many :referring_relationships, foreign_key: "referring_id", 
                                    class_name:  "Reference",
                                    dependent:   :destroy
    has_many :referring, through: :referring_relationships, source: :referring
  end
  </code></pre>
  
  <p>Posts_Controller.rb</p>
  
  <pre><code>def show
    @title = "Post Profile"
    @post = Post.find(params[:id])
  end
  
  def post_params
    params.require(:post).permit(:title, :referred_id)
  end
  </code></pre>
  
  <p>Posts/show.html.erb (Posts profile page)</p>
  
  <pre><code>WHAT CODE WOULD GO HERE TO LOOP THROUGH REFERRING POSTS SHOWING POST TITLES???
  
  &lt;div class="postA"&gt;              
       &lt;!-- Title --&gt;
      &lt;h2&gt;
        Post &lt;%= @post.id %&gt;
      &lt;/h2&gt;
  
      !-- Content --&gt;
      &lt;p class="text-muted"&gt;
        "&lt;%= @post.title %&gt;"
      &lt;/p&gt;
  &lt;/div&gt;
  
  WHAT CODE WOULD GO HERE TO LOOP THROUGH REFERRED POSTS SHOWING POST TITLES???
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Thanks I've tried what you suggested as seen below:</p>
  
  <pre><code> &lt;% if @post.referred.any? %&gt;
  
              &lt;div class="card"&gt;
  
              &lt;% @referred.each do |referred_post| %&gt;
                &lt;p&gt;&lt;%= referred_post.title %&gt;&lt;/p&gt;
              &lt;% end %&gt;
  
                &lt;br clear="all"&gt;
  
              &lt;/div&gt;
  
        &lt;% else %&gt;
  
              &lt;p&gt;no relationships exist&lt;/p&gt;
  
       &lt;% end %&gt;
  </code></pre>
  
  <p>But unfortunately it is only showing the original posts title looped for each relationship (as opposed to the title of the referring or referred post.  </p>
  
  <p>So it just loops through the original post's title as seen here:</p>
  
  <ul>
  <li>Post A</li>
  <li>Post A</li>
  <li>Post A</li>
  </ul>
  
  <p>UPDATE 2:</p>
  
  <p>Schema.rb:</p>
  
  <pre><code>create_table "posts", force: :cascade do |t|
      t.string "title"
      t.bigint "user_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.string "url"
      t.index ["user_id"], name: "index_posts_on_user_id"
    end
  
  create_table "references", force: :cascade do |t|
      t.integer "referred_id"
      t.integer "referring_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
      t.index ["referred_id", "referring_id"], name: "index_references_on_referred_id_and_referring_id", unique: true
      t.index ["referred_id"], name: "index_references_on_referred_id"
      t.index ["referring_id"], name: "index_references_on_referring_id"
    end
  </code></pre>