循环通过在Rails中具有许多直通关系

时间:2018-12-01 07:03:08

标签: ruby-on-rails ruby for-loop ruby-on-rails-5

我有一个Post模型,然后有一个Reference模型,用作两个Post之间关系的模型。因此,单个帖子(例如POST A)将包含一些其引用的“推荐”帖子(POST B,POST C等),并且还将包含一些“推荐”帖子(POST D,POST E,等)。从POST A的“显示”视图来看,我只是试图遍历并显示所有“被引用”的帖子以及其上方和下方的所有“引用”帖子。我想使用参考关系模型来做到这一点。创建帖子后,成功创建了references&referred_relationships / referring_relationships,但是我无法找出正确的代码放置在视图中以在列表中显示这些相关的帖子标题...对于每一个的帮助,将不胜感激下面提到的循环应如下所示:

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(“帖子”个人资料页面)

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???

更新:

谢谢我尝试了您的建议,如下所示:

 <% 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 %>

但是不幸的是,它仅显示了每种关系循环的原始帖子标题(与推荐或推荐帖子的标题相对。

因此,它只是遍历原始帖子的标题,如下所示:

  • 发布A
  • 发布A
  • 发布A

更新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

1 个答案:

答案 0 :(得分:1)

在控制器中,您可以将相关记录加载到实例变量中,然后从控制器中循环遍历它们。

控制器:

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

视图:

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

您可以使用@referred做同样的事情。