我爲我的訂購應用程序使用了Cancan,Devise,Rails 3。CanCan限制發現數據
每個用戶都有許多公司通過協議。每家公司也有許多用戶通過協議。
在我的能力模型中,我有以下幾點:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :super
can :manage, :all
elsif user.role? :admin
can :manage, [User, Company, Order]
elsif user.role? :tech
can :manage, [User, Company, Tech]
elsif user.role? :customer_admin
can [:read, :update], User, :id => user.id
can [:read, :update], Company, :id => user.id
can [:read ], Order, :id => user.id
end
end
end
當,我一直在試圖告訴他們只有他們正在與相關公司一customer_admin日誌。在公司的觀點中,我可以看到用戶列表就好。
在我公司的控制器(指數),我試着這樣做:
@usercompanies = Company.where(['user_id = ?', current_user.id ])
然而,這列出了錯誤的公司?
肯定這是一個愚蠢的新手錯誤,但會感謝您的幫助。如果你需要別的東西,讓我知道。
此外,在我的回答中,您確實希望保留行'can [:read,:update],User,:id => user.id',因爲用戶模型的id和current_user id是相同的。所有其他記錄需要根據我的答案進行修改。 – 2011-06-11 14:39:54