I have an app where users are approved by an admin after registering. I need a little help filtering out the admins in the members list.
This is my index method:
def index
if params[:is_approved].to_i == 0
@users = User.find_all_by_is_approved(0) #returns all unapproved users
else
@users = User.find_all_by_is_approved(1) #here I wanna filter out the admins
end
end
I tried to add && User.find_all_by_is_admin(0) but then I get all the users that are not admins, i.e., the unapproved users.
Admins are users but have a column is_admin set to true, and I dont want to display them.
If you want the list of users who are approved but aren’t admins:
or if you want to write it in the style you were using you could do:
The reason why what you’re trying isn’t working is because
&&is the boolean AND operator so if you writeBoth sides of the
&&here are considered true so this evaluates to the right-hand side i.e.User.find_all_by_is_admin(0)There is an
&(single & sign) operator on arrays in Ruby that gives the intersection of 2 arrays i.e.would be the list of users who are in both lists, but this is an inefficient way of doing it as it results in 2 separate database queries.