ruby - Rails 3 / Devise: Checking to see if users have any of multiple roles -


I have a user model with the devices, and I have established the roles so that the user assigned one or more role Can go If I say to check the role of the user in a scene, the following code works ...

   You are an administrator! & Lt;% end% & gt;   

But now, suppose I want to see that the user has many roles, to see if he is an administrator or editor. I was certain that the following would work ....

     

... but it is not so, it only works if you are an administrator and ignored checking that list of all other roles after the first one in that list. I have tried Role.find_by_name (['Administrator', 'Editor']) and other combinations for not being successful. In fact, how do I format it to check for a number of formats? I have hunted all over Google, but all the examples have a role as well.

The first problem is that when you use the syntax 'Administrator' 'Editor' will not create a SQL or syntax instead, instead it evaluates Ruby that the 'Administrator' is considered a valid value and sends it to the find_by_name method, the editor never sends.

The other problem is that if you change the syntax to ['Administrator', 'Editor'], the find_by_name method always returns the same record.

Then I will do something like this:

  role. Where (: name = & gt; ['administrator', 'editor']). All   

He will return those two roles.

Edit

I thought more about this because it is not really the best practice solution to keep such a database logic in these ideas. Here's another way that is slightly more with the MVC concept:

  class users & lt; ActiveRecord :: Base ... def is_role? (* Role_name) self.roles.where (: name = & gt; role_names). Present? End # end & lt;% if current_user.has_role? ('Administrator', 'Editor')% & gt;    

Comments