Ruby on Rails Thursday, May 31, 2012

I'm trying to use CanCan to an account scoped application. I wrote this method in my ability class:


    roles = user.roles.find_by_account_id(account.id)
    if roles.any? { |role| role.type == "Ownership" }
      can :edit, Account
    else
      can :read, :all
    end

The trouble is, "any?" works when "roles" is an array. Sometimes, however, user.roles returns only one object any other times my return nil. So calling roles.any? causes an error. How do I work around this?

My setup is User has_many Accounts through :roles, and vice-versa.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/mORQoYLqDtwJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment