gpt4 book ai didi

ruby-on-rails - _mask 和 Rails

转载 作者:太空宇宙 更新时间:2023-11-03 16:39:20 25 4
gpt4 key购买 nike

所以我正在尝试获取 cancan gem 与我的 Rails 3 应用程序一起工作,但我遇到了问题。

我试图复制 Ryan Bates( gem 的创造者)在他的 screen cast 中使用的代码,但我收到一条错误消息,指出 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

谢谢

最佳答案

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

来自较早的一集 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

关于ruby-on-rails - _mask 和 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2478536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com