Sql 自定义提要,仅显示跟踪用户的文章

Sql 自定义提要,仅显示跟踪用户的文章,sql,ruby-on-rails,Sql,Ruby On Rails,我一直在参考rails教程,试图创建一个提要,其中只有属于接受我朋友请求的用户的文章才会出现在我的提要中 然而,我有一个不同于教程的模式和用户模型(我还有一个额外的步骤,在这个步骤中,朋友请求等待接受)。我的用户模型中的以下方法不会过滤掉未被接受的友谊,因此未经确认的朋友的文章出现在我的提要中,这不是我想要的 user.rb has_many :friendships has_many :received_friendships, class_name: "Friendship", fo

我一直在参考rails教程,试图创建一个提要,其中只有属于接受我朋友请求的用户的文章才会出现在我的提要中

然而,我有一个不同于教程的模式和用户模型(我还有一个额外的步骤,在这个步骤中,朋友请求等待接受)。我的用户模型中的以下方法不会过滤掉未被接受的友谊,因此未经确认的朋友的文章出现在我的提要中,这不是我想要的

user.rb

  has_many :friendships
  has_many :received_friendships, class_name: "Friendship", foreign_key: "friend_id"
  has_many :active_friends, -> { where(friendships: { accepted: true}) }, through: :friendships, source: :friend
  has_many :received_friends, -> { where(friendships: { accepted: true}) }, through: :received_friendships, source: :user
  has_many :pending_friends, -> { where(friendships: { accepted: false}) }, through: :friendships, source: :friend
  has_many :requested_friendships, -> { where(friendships: { accepted: false}) }, through: :received_friendships, source: :user

  def feed
    friend_ids = "SELECT friend_id FROM friendships
                     WHERE  user_id = :user_id"
    Article.where("user_id IN (#{friend_ids})
                     OR user_id = :user_id", user_id: id)
  end
  create_table "friendships", force: :cascade do |t|
    t.integer "user_id"
    t.integer "friend_id"
    t.boolean "accepted", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
<%= render @feed %>
schema.rb

  has_many :friendships
  has_many :received_friendships, class_name: "Friendship", foreign_key: "friend_id"
  has_many :active_friends, -> { where(friendships: { accepted: true}) }, through: :friendships, source: :friend
  has_many :received_friends, -> { where(friendships: { accepted: true}) }, through: :received_friendships, source: :user
  has_many :pending_friends, -> { where(friendships: { accepted: false}) }, through: :friendships, source: :friend
  has_many :requested_friendships, -> { where(friendships: { accepted: false}) }, through: :received_friendships, source: :user

  def feed
    friend_ids = "SELECT friend_id FROM friendships
                     WHERE  user_id = :user_id"
    Article.where("user_id IN (#{friend_ids})
                     OR user_id = :user_id", user_id: id)
  end
  create_table "friendships", force: :cascade do |t|
    t.integer "user_id"
    t.integer "friend_id"
    t.boolean "accepted", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
<%= render @feed %>
页面控制器

def home
    @feed = current_user.feed.all
end
home.html.erb

  has_many :friendships
  has_many :received_friendships, class_name: "Friendship", foreign_key: "friend_id"
  has_many :active_friends, -> { where(friendships: { accepted: true}) }, through: :friendships, source: :friend
  has_many :received_friends, -> { where(friendships: { accepted: true}) }, through: :received_friendships, source: :user
  has_many :pending_friends, -> { where(friendships: { accepted: false}) }, through: :friendships, source: :friend
  has_many :requested_friendships, -> { where(friendships: { accepted: false}) }, through: :received_friendships, source: :user

  def feed
    friend_ids = "SELECT friend_id FROM friendships
                     WHERE  user_id = :user_id"
    Article.where("user_id IN (#{friend_ids})
                     OR user_id = :user_id", user_id: id)
  end
  create_table "friendships", force: :cascade do |t|
    t.integer "user_id"
    t.integer "friend_id"
    t.boolean "accepted", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
<%= render @feed %>

对于您的案例,我认为关键在于这里(user\u id=:user\u id AND accepted=true),我从友谊模式中获取accepted字段

  def feed
    friend_ids = "SELECT friend_id FROM friendships
                     WHERE (user_id = :user_id AND accepted = 't')"
    Article.where("user_id IN (#{friend_ids})
                     OR user_id = :user_id", user_id: id)
  end