Rails-使用关联定义康康能力 [英] Rails - defining cancan ability using associations
本文介绍了Rails-使用关联定义康康能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在cancan Ability类中定义能力时...
When defining abilities in the cancan Ability class...
这是
can :manage, Area, :location => { :company => { :manager => { :user_id => user.id } } }
与此相同:
can :manage, Area do |area|
area.location.company.manager.user_id == user.id
end
我只是想更好地理解不使用障碍物就定义一个异能.一种方法(也许更快)比另一种更好吗?
I'm just trying to better understand defining an ability without using a block. Is one way better (maybe faster) than the other?
推荐答案
两者应该是相同的,但是如果其他人正在阅读您的代码,则第二种方法会更加清晰.我绝对会建议使用第二种方法.如果困扰您的是多行,您可以这样写:
The two should be the same, however the second way is much clearer if anyone else is reading your code. I would definitely recommend using the second way. If what bothers you is taking up multiple lines, you could just write:
can( :manage, Area ) {|area| area.location.company.manager.user_id == user.id }
(与其他规则堆叠在一起时),这可能比任何其他选择都更清洁.
That's possibly cleaner (when stacked with a bunch of other rules) than either other option.
这篇关于Rails-使用关联定义康康能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文