【问题标题】:display all users that current_user don't follows显示 current_user 没有关注的所有用户
【发布时间】:2013-11-05 16:58:37
【问题描述】:

我正在开发一个与 Michael Hartl 的 Railstutorial 类似的示例应用程序,并希望显示 current_user 关注的所有用户。我该怎么做?

User.rb 是:

has_many :following_relationships, class_name: "Relationship", foreign_key: :follower_id
has_many :leading_relationships, class_name: "Relationship", foreign_key: :leader_id

Relationship.rb 是:

belongs_to :follower, class_name: "User"
belongs_to :leader, class_name: "User"

【问题讨论】:

  • 你用的是什么导轨?

标签: ruby-on-rails activerecord ruby-on-rails-4 relationship railstutorial.org


【解决方案1】:

这行得通吗?

@followingusers = 你所关注的用户所拥有的一切

@notfollowingusers = User.where("id 不在 (?)", @followingusers.map(&:id))

【讨论】:

  • 是的,它可以工作,但我怎样才能将当前用户包含到未关注的用户中?
  • 试试这个。 @notfollowingusers = User.where("id 不在 (?) AND id not (?)", @followingusers.map(&:id), current_user.id)
  • 或者你可以只做@followingusers
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多