2010-03-19 143 views
2

所以我试图让cancan宝石与我的rails 3应用程序一起工作,并且遇到了问题。_mask and Rails

我试图复制在他的screen cast中使用的Ryan Bates(创建者的宝石)的代码,但我得到一个错误,说roles_mask不是一种方法。我认为_mask方法在某些时候从Ruby/Rails中被移除了,现在我想知道什么是替换。

下面是我的user.rb型号代码:

named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} } 

    ROLES = %w[admin student principal admissions] 

    def roles=(roles) 
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum 
    end 

    def roles 
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? } 
    end 

    def role? 
    roles.include? role.to_s 
    end 

    def role_symbols 
    roles.map(&:to_sym) 
    end 

我用Rails 3和Ruby 1.9.2dev

谢谢

回答

1

这听起来像你的用户表缺少roles_mask列,你确定你已经包含它并迁移你的数据库吗?

从更早的情节http://asciicasts.com/episodes/189-embedded-association

我们要做的第一件事情就是添加一个新的 整数列叫roles_mask到 用户表来存储位掩码 值。

script/generate migration add_roles_mask_to_users roles_mask:integer 

然后运行迁移

rake db:migrate 
+0

哇,应该只是看过的插曲,而不是走过场asciicasts的。非常感谢。 – 2010-03-20 00:25:40