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")
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?
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.