简体   繁体   中英

Loop through has many through relationship in Rails

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 & 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:

Reference.rb

class Reference < ApplicationRecord 
  belongs_to :referred, class_name: "Post"
  belongs_to :referring, class_name: "Post"
  validates :referred_id, presence: true
  validates :referring_id, presence: true
end

Post.rb

class Post < 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

Posts_Controller.rb

def show
  @title = "Post Profile"
  @post = Post.find(params[:id])
end

def post_params
  params.require(:post).permit(:title, :referred_id)
end

Posts/show.html.erb (Posts profile page)

WHAT CODE WOULD GO HERE TO LOOP THROUGH REFERRING POSTS SHOWING POST TITLES???

<div class="postA">              
     <!-- Title -->
    <h2>
      Post <%= @post.id %>
    </h2>

    !-- Content -->
    <p class="text-muted">
      "<%= @post.title %>"
    </p>
</div>

WHAT CODE WOULD GO HERE TO LOOP THROUGH REFERRED POSTS SHOWING POST TITLES???

UPDATE:

Thanks I've tried what you suggested as seen below:

 <% if @post.referred.any? %>

            <div class="card">

            <% @referred.each do |referred_post| %>
              <p><%= referred_post.title %></p>
            <% end %>

              <br clear="all">

            </div>

      <% else %>

            <p>no relationships exist</p>

     <% end %>

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.

So it just loops through the original post's title as seen here:

  • Post A
  • Post A
  • Post A

UPDATE 2:

Schema.rb:

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

In the controller you can load the related records into an instance variable and then loop through them from the controller.

controller:

def show
  @title = "Post Profile"
  @post = Post.find(params[:id])
  @referring = @post.referring
  @referred = @post.referred
end

view:

<ul>
<% @referring.each do |referring_post| %>
  <li><%= referring_post.title %></li>
<% end %>
</ul>

You can do the same thing with @referred .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM