I have a function which takes * parameter as for passing an array.
def has_role?(*role_names)
self.roles.where(:name => role_names).present?
end
where I can call it like:
user.has_role?(['super admin', 'member'])
or like
user.has_role?('super admin', 'member')
or like
user.has_role?('member')
When I have two parameters to pass, how can I do it with a list and a parameter?
For example, I have a another function the scopes down a similar query:
def has_account_role?(role_names, account)
self.account_user_roles.joins(:role).where('roles.name = ? AND account_id = ?', role_names, account.id).present?
end
If I add a * to has_account_role like this has_account_role?(*role_names, account) and call user.has_account_role?(['member', 'supervisor'], account) I get the error: can't quote Array
I tried changing the query from = ? to IN (?) but get the same issue. If I try to change the query to something like ...where(:name => role_names)... I lose the scoping that role.name provides the filtering on the association.
How do I pass an array into this query without making it overly complete or inefficient?