I'm working on a CRUD interface for managing users in my application. I have a constant User::ROLES that is an array with the names of valid user roles in my app (admin, teacher, student).
What I'm wanting to do is, in the index action of the controller, have a block that iterates through the ROLES constant and creates a scoped instance variable from @users (which is already initialized before the block). Here's what the index method looks like so far:
def index
@users = user.all
#@students = @users.where(:role => "student") # This works by itself
User::ROLES.each do |r|
@r = @users.where(:role => r.to_s)
end
end
So I want to be able to name the instance variable by what's passed through the block, so @r creates @admin, @teacher, etc. Since I'm new to ruby and rails, I don't quite understand the syntax for doing this. Who knows? There's probably a better way to do it anyways.
EDIT
Just to let everyone know, I'm hoping to use this in the view on the index action to display a list of users grouped by their role. Of course, it will be used in other ways too throughout the CRUD interface, which is why I didn't clarify the use-case earlier, since it's multi-purpose.