0

I'm using cancan in a Rails 3.2 app, and in the abilities.rb classing have a method for each role's abilities

#ability.rb

def initialize(user)
  user_or_admin ||= User.new

  user.roles.each do |role| 
    send(role.name)
  end
end

def role_name_a

end

def role_name_b

end

Some of these methods require access to the user record, how do I pass it with the send function?

e.g. I want to do something like

send(role.name)(user)

and then

def role_name_a(user)

But this doesn't work. Grateful for any ideas.

2 Answers 2

2

The arguments are the second parameter to send. The first argument is the method name as a symbol.

send :"#{role.name}", user

You should try looking at Ruby's documentation for questions like this. The language is well documented.

http://ruby-doc.org/core-1.9.3/Object.html#method-i-send

Sign up to request clarification or add additional context in comments.

1 Comment

great, thanks for the reference, I was looking in the wrong place!
0

I'm not sure if this is best practice but it's what is in the cancan documentation.

def initialize(user)
  @user = user || User.new

  user.roles.each do |role| 
    send(role.name)
  end
end

def role_name_a
  can :manage, User, id: @user.id
end

def role_name_b

end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.