NoMethodError when using any?

7 views
Skip to first unread message

Mohamad El-Husseini

unread,
May 31, 2012, 1:04:20 PM5/31/12
to rubyonra...@googlegroups.com
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.

Everaldo Gomes

unread,
May 31, 2012, 1:22:42 PM5/31/12
to rubyonra...@googlegroups.com
Hi!

There are many ways to solve this problem.

One sugestion, maybe not the better: you can use the method kind_of?
to see if roles is a Array.

roles.kind_of? Array
(will return true)

Then, you could build your logic upon this.

Best Regards,
Everaldo
> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.

Mohamad El-Husseini

unread,
May 31, 2012, 1:25:49 PM5/31/12
to rubyonra...@googlegroups.com
Another way would be to change:

roles = user.roles.find_by_account_id(account.id)

to

roles = user.roles.where(account_id: account.id)
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to

Mohamad El-Husseini

unread,
May 31, 2012, 1:26:50 PM5/31/12
to rubyonra...@googlegroups.com
I forgot to mention that where() returns an array where as find_by_foo returns a single object unless there are multiple objects...

-----

Another way would be to change:

roles = user.roles.find_by_account_id(account.id)

to

roles = user.roles.where(account_id: account.id)

Everaldo Gomes

unread,
May 31, 2012, 1:42:23 PM5/31/12
to rubyonra...@googlegroups.com
Nice, I forgot about that too...

On Thu, May 31, 2012 at 2:26 PM, Mohamad El-Husseini
>>> > To post to this group, send email to rubyonra...@googlegroups.com.
>>> > To unsubscribe from this group, send email to
>>> > rubyonrails-ta...@googlegroups.com.
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> 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/-/o9ZLULIE9hoJ.
>
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages