0
我正在關注本教程http://blog.endpoint.com/2012/01/ruby-on-rails-rights-attributes.html以爲我的rails4應用程序設置授權機制。rails4中的多態關聯
我已經創建了數據模型,但不管理(從控制檯)獲取用戶的權限(通過all_rights方法)。
在用戶模型中,User對象如何通過RightAssignment調用「self.rights」權限,而不是直接來自User?
我的模型:
class User < ActiveRecord::Base
has_many :right_assignments, as: :subject
has_and_belongs_to_many :groups
has_and_belongs_to_many :roles
def all_rights
rights = [self.rights +
self.groups.collect { |g| g.allowed_rights } +
self.roles.collect { |r| r.rights }]
rights = rights.flatten.uniq.collect { |r| r.action }
rights
end
end
class Group < ActiveRecord::Base
has_many :right_assignments, as: :subject
has_and_belongs_to_many :users
def allowed_rights
self.assignable_rights ? self.rights : []
end
end
class Role < ActiveRecord::Base
has_many :right_assignments, as: :subject
has_and_belongs_to_many :users
end
class RightAssignment < ActiveRecord::Base
belongs_to :right
belongs_to :subject, polymorphic: true
end
class Right < ActiveRecord::Base
has_many :right_assignments
end
任何想法?