This is the case:
A project has many users.
a project has an admin.
when I list the project users I don’t want to display the admin user so
what I tried was:
users = @project.users
users.delete(admin_user)
users.each { |u| {
…
}
but what happens is that the admin_user gets deleted from the project.
is there a way to do a temporary delete without activerecord acting upon
it and without having to check with an ‘if’ inside the for loop?
in your table you must have an field, that defines the role of the user.
either an bool flag like is_admin, that can be true or false or a
integer or string, with several states like admin, customer and so on.
with a is_admin flag:
users = @project.users.find_by_is_admin(false)
with an integer role (0=normal user, 1 = admin)
users = @project.users.find_by_role(0)
with a string role (“admin” = admin, “user” = user…)
users = @project.users.find_by_role(“user”)
I though of that suggestion but the problem is that there is a
“permissions” table in between user and project which keeps the ‘admin’
boolean.
Yes it can be queried as well but I thought that would be just
performance loss just to filter out one user.
Getting all users of a project and just subtracting the admin_user
(which I already have) seemed a better solution.
Is there really no way for this similar to the way i did it:
users = @project.users
users.delete(admin_user)
but then without actually deleting the record from DB?
Very strange, as this should work just fine. I too would expect this
to perform an Array#delete, not an ActiveRecord::Base.delete, and
testing this in one of my projects worked fine.
Can you verify that the result of @project.users is indeed an array?
Very strange, as this should work just fine. I too would expect this
to perform an Array#delete, not an ActiveRecord::Base.delete, and
testing this in one of my projects worked fine.
Can you verify that the result of @project.users is indeed an array?
It’s not an array, it’s an association proxy.
u = @project.users
u.delete(foo)
is the same as @projects.users.delete(foo) (indeed there would be some
very funky going on if it wasn’t).
In this case you could do something like
users_without_admin = @project.users.reject {|u| u == admin}
or of course arrange for admin not to be in @project.users in the
first place.