Answer the question
In order to leave comments, you need to log in
How to add your method to a Rails application?
Hello. My question is if a user has a :role attribute that has different values. How do I add a method like user.admin? that returns something like self.role == "admin"?
The bottom line is that this method should work in relation to what you call it on. For example.
current_user.admin? (true/false)
@user.admin? (where @user is assigned in the controller) (true/false)
Thank you very much for your help. I appreciate that you took the time.
Answer the question
In order to leave comments, you need to log in
# models/User.rb
class User
...
def admin?
self.role == 'admin'
end
...
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question