具有嵌套资源的CanCan gem和:find_by

时间:2011-10-17 02:20:52

标签: ruby-on-rails-3.1 cancan

我使用CanCan load_and_authorize_resource辅助方法获取资源并生成授权,但我有一个像这样的嵌套资源

load_and_authorize_resource :company
load_and_authorize_resource :accountings, :through => :company, :class => 'Departments::Accounting'

但我需要 @accountings可以通过另一个属性找到而不是Departmets :: Accounting id并为该属性赋值,例如

@accountings = @company.find_by_period_id(@period.id)

1 个答案:

答案 0 :(得分:1)

你可以通过两个额外选项来实现:

load_and_authorize_resource :accountings,
    :through => :company, :class => 'Departments::Accounting',
    :find_by => :attr, # :period_id in your example
    :id_param => :something # this searches the params hash for
                            # params[:something], and sends that
                            # value to .find_by_attr

检查load_and_authorize_resource here的代码。