Rails Activerecord: Find All Users Except Current User

Rails ActiveRecord: Find All Users Except Current User

It is possible to do the following in Rails 4 and up:

User.where.not(id: id)

You can wrap it in a nice scope.

scope :all_except, ->(user) { where.not(id: user) }
@users = User.all_except(current_user)

Or use a class method if you prefer:

def self.all_except(user)
where.not(id: user)
end

Both methods will return an AR relation object. This means you can chain method calls:

@users = User.all_except(current_user).paginate

You can exclude any number of users because where() also accepts an array.

@users = User.all_except([1,2,3])

For example:

@users = User.all_except(User.unverified)

And even through other associations:

class Post < ActiveRecord::Base
has_many :comments
has_many :commenters, -> { uniq }, through: :comments
end

@commenters = @post.commenters.all_except(@post.author)

See where.not() in the API Docs.

how to select all users except for the current user

User.where(user: != current_user) is invalid syntax, for SQL comparison operators you can provide a string as an argument for where:

User.where('user_id != ?', current_user.id) # binding the argument provided

Or just User.where.not(id: current_user.id)

how can I display all users except current user and current_user's friends in ruby on rails

To display ALL users that are NOT current_user OR current_users_friends, this should work for you:

scope :all_except, ->(user) { where(["id NOT IN (?)", ([user.friends.map(&:id) << user.id]) ]) }

Rails options_from_collection_for_select display all users except current user

Controller

class UserController < ApplicationController
def index
@users = User.where.not(:id=>current_user.id)
end
end

You can access instance variable @users in your views

Or

You can use scope variable given below my explanation how to use

Model

class User < ApplicationRecord
scope :all_except, ->(user) { where.not(id: user) }
end

in your controller access the scope variable

controller

@users = User.all_except(current_user)

How I get all users except the current_user by username

If your current_user has set in application_controller.rb you can create a method there that list all users without him like:

class ApplicationController < ActionController::Base
...
def all_users
User.where.not id: current_user.id
end
end

Whatever, you could use:

User.where.not username: current_user.username

Return all user profile except current users'

The line n = m.country.class == String sets n to true or false, thefore the next line fails, because you try to call reject! {|x| x == current_user.profile.user_id } on that boolean value.

One way to solve the problem might be to fix your code like this:

def method_name
Profile.all.select do |p|
p.country.present? && p != current_user.profile
end
end

Another way - and my preferred one - would be to only load matching record from the database:

def method_name
Profile.where.not(user_id: current_user.id).where.not(country: nil)
end

How to select all users except ids

Try:

User.where.not(id: params[:ids])

Here params is the parameter sent to server during the request, and values in params then can accessed with keys, in your case it is :ids: params[:ids] not :id(params[:id]).

Rails & Active Record: Find objects whose children don't belong to current_user OR who don't have any children

Hacky but fun answer

class Event
has_many :votes
has_many :user_votes,
->() { where({user_id: Thread.current[:user]&.id}.compact) },
class_name: 'Vote'

def self.using_user(user)
old_user, Thread.current[:user] = Thread.current[:user], user
yeild
ensure
Thread.current[:user] = old_user
end
end

Event.using_user(current_user) do
Event.includes(:user_votes).where("votes.id IS NULL")
end

Original answer:

Event.
joins(
"LEFT OUTER JOIN votes ON votes.event_id = events.id AND " +
"votes.user_id = #{user.id}"
).
where("votes.id IS NULL")

RoR Filter Out Current User with Join, Select and Order

This should do the trick:

@users = ...
@users = @users.where.not(users: {id: current_user.id})

Note that you need to specify name of the table (not the model) when you do the join, otherwise database will have no idea which id column it should look for (users.id vs departments.id).

Before Rails 4

not method is quite a new thing and is not available in Rails 3. (In fact, where method wasn't expecting to be called without arguments, that's why the error you got is unexpected number of arguments (0 for 1)).

Those were dark times when we had to use the following monstrosity to get stuff working:

@users = ...
@users = @users.where('users.id != ?', current_user.id)

Rails: Getting the All Users Where User has not followed him

you can do it by following way

first find ids of all the users that current_user is following

ids = current_user.followings.pluck(:id)

and then find all the users except that ids

users = User.where("id NOT IN ?", ids)

This might be help you



Related Topics



Leave a reply



Submit