我有使用设备和 cancancan 的用户模型。这是我的能力:
if user.role? :patient
can :access_profile, User, :id => user.id
end
其中 access_profile 别名是:
alias_action :show, :edit, :update, :destroy, :to => :access_profile
在我的 users_controller 中,我使用 load_and_authorize_resource。例如,当我尝试“users/4”和 current_user.id = 3 时,我能够到达那里。
在日志中我看到这个:
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 4]]
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
Rendered users/show.html.erb within layouts/application (0.4ms)
这里第一个查询来自 set_user 方法。我猜第二个查询会加载我的资源以进行授权。在这里,由于 current_user.id = 3 并且在第二个查询中我也看到了 3,我想这就是我被授权的原因。但我猜,第二个查询中的 id 必须是 4。
怎么做才能使 load_and_authorize 方法正常工作。