If I have a class called Group, and in that class I have a
has_many :users
I want to add a method to the Group class to see if a specific user is in that group. If I’m using the Devise authentication I can call the current_user.
So I guess my first question would be can I call that current_user from the model, or do I need to pass it to the method?
My second question is I should be able to do something like the following?
def has_access?
!current_user.nil && users.where(:id == current_user.id).count(:id) > 0
end
You can’t call current_user in models – it’s only available within the scope of controllers and views.
Which means your method’s going to look a little different – perhaps try the following: