NOT SQL queriesを使用すると、クエリからレコードを除外できます。
User.where.not(id: current_user.id).order_by_rand.limit(3)
そして、これは再利用可能であることを行っている場合は、あなたが先に行くと、以下のような単純なスコープを作成することができます。
# user.rb
# ex: User.exluding(current_user).order_by_rand....
scope :excluding, (lambda do |user|
return all unless user.present?
where.not(id: user.id)
end)
Thoughtbotの人々はまた、クエリのこれらの種類にgreat articleを持っています。フォロワーを除外するために
:以下のコメントから
更新
# user.rb
# This will also exclude the user you pass in.
# so you will not need to chain the original scope `excluding`.
scope :recommended_users, (lambda do |user|
return all unless user.present?
where.not(id: user.following_ids << user.id)
end)
動作しますが、どのようにも、現在のユーザーが、次のされていないユーザーを見つけることについて?私は '.followers.include(!current_user)'のようなものを試していますが、うまくいかないようです。 'do | user | 'のように、各ユーザを繰り返し処理する必要がありますか? ...?私はここに 'acts_as_follower'の宝石を使用しています –
@omegalo、更新された答えを見てください。これは、あなたがフォローしているユーザーを表す 'following'という関係を持っていると仮定しています。 –
これは現在のユーザを除外しているようですが、 'following_ids'は他の2人のユーザに続いてコンソールにnilを返します。私が従っているユーザーはまだどんなアイデアを示していますか? –