Check if any of multiple conditions are true in Ru

2019-09-12 11:59发布

I have the following Ruby conditional:

<% if can? :read, %w(policy journey crash user).map(&:to_sym) %>

Which I want to translate to: if the user has read permissions for any of the resources in the array.

However it always returns false. How can I fix it?

I don't want to do:

if can? :read, :policy || can? :read, :journey || etc...

1条回答
爷的心禁止访问
2楼-- · 2019-09-12 12:37

Sure you can.

Enumerable#any? is exactly what you're looking for:

<% if %i(policy journey crash user).any? { |action| can? :read, action } %>

The above will return true only if can read any action.

Note, I used %i instead of %w. It gives you array of symbols.

查看更多
登录 后发表回答