【发布时间】:2016-07-03 02:20:08
【问题描述】:
我想列出所有current_user 未关注的用户,以便 current_user 可以关注他
用户模型如下所示:
class User < ActiveRecord::Base
has_many :follower_relationships, foreign_key: :following_id, class_name: 'Follow'
has_many :followers, through: :follower_relationships, source: :follower
has_many :following_relationships, foreign_key: :follower_id, class_name: 'Follow'
has_many :following, through: :following_relationships, source: :following
end
Follow 类看起来像这样。
class Follow < ActiveRecord::Base
belongs_to :follower, foreign_key: 'follower_id', class_name: 'User'
belongs_to :following, foreign_key: 'following_id', class_name: 'User'
end
我知道我可以像这样得到以下所有内容:
current_user.following
我想获取所有未关注 current_user 的用户。 我试图这样做,但我做不到:(
User.all.where.not(following: current_user.following)
如果你没有正确理解问题,我会尝试给出以下示例。
假设有用户 A , B , C , D ,E 。假设 A 关注 B 和 C 。我想显示D 和E,以便A 知道他没有关注D 和E。
谢谢
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord