显示的用户帖子不正确。 (追随者和追随者)

时间:2014-08-20 21:41:39

标签: ruby-on-rails post ruby-on-rails-4 twitter-follow

我正在尝试显示我关注的用户的帖子。但是,出于某种原因,我的流索引显示来自跟随我的用户的帖子。如何显示我关注的用户的帖子,而不是相反?提前谢谢。

用户模型

has_many :following, :through => :relationships, :source => :followed
has_many :subscribed, class_name: "Relationship", foreign_key:  "follower_id"

发布模型

scope :subscribed, ->(following) { where user_id: following }

关系模型

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true

  has_many :followed_users, through: :relationships, source: :followed

  #fields id | user_id | follower_id | created_at | updated_at
  belongs_to :user
end

流控制器

class StreamController < ApplicationController
  def index
    @posts = Post.subscribed current_user.following 
  end 
end

流索引

<div class="page-header">
  <center><strong><h1> Stream Page </h1></strong></center>
</div>

 <div id="posts" class="transitions-enabled">
  <% @posts.each do |post| %>

    <div class="box panel panel-default">
      <%= link_to image_tag(post.image.url(:medium)), post %>
      <div class="panel-body">
      <%= post.description %><br/>
      <strong><%= post.user.name if post.user %></strong>
     <% end %>
    </div>
    <% end %>
    </div> 
  <% end %>
 </div>

1 个答案:

答案 0 :(得分:1)

问题在于您正在调用的关联

-

<强>协会

你正在使用我的代码(这很棒!) - 我认为问题在于你如何调用relationship关联数据

你将能够做到这一点:

#app/controllers/posts_controller.rb
Class PostsController < ApplicationController
   @posts = Post.subscribed current_user.followed 
end