开发者

_mask and Rails

So I am trying to get the cancan gem to work with my rails 3 app and I've hit a problem.

I tried to copy the code that Ryan Bates (the creator of the gem) used in his screen cast, but I get an error saying that roles_mask is not a method. I figure that the _mask method was removed from Ruby/Rails at some point, and I'm now wondering what is the replacement.

Here's the code in my user.rb model:

  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

I'm using Rails 3 and Ruby 1.9.2dev

Thank you


It sounds like your users table is missing the roles_mask column, are you sure you have included it and migrated your database?

From the earlier episode http://asciicasts.com/episodes/189-embedded-association:

The first thing we’ll do is add a new integer column called roles_mask to the users table to store the bitmask value.

script/generate migration add_roles_mask_to_users roles_mask:integer

Then run the migration

rake db:migrate
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜