【发布时间】:2014-06-19 19:04:57
【问题描述】:
我的 Rails 模型有一些不好的线条。一般来说,第一个错误是从has_access def 访问roles 变量时。然后,第二个是在进行where 操作时。
class Organization < ActiveRecord::Base
has_and_belongs_to_many :organizations_users
belongs_to :user
validates :name, :presence => true, :length => { minimum: 4, maximum: 35 }, uniqueness: true
roles = { :administrator => 1, :support => 2 }
def has_access(chosen_user, role)
self.organizations_users.where('user_id = ? and role = ?', chosen_user.id, roles[role]).exists?
end
def add_user(chosen_user, role)
if !self.has_access(chosen_user, role)
self.organizations_users.create({user_id: chosen_user.id, role: roles[role]})
end
has_access(chosen_user,role)
end
end
我还需要查询organizations_users 表以获取有关访问的信息。我该如何解决?
【问题讨论】:
-
... 有什么错误?
-
For where 语句 (2nd) 是 #<0x47b0038>0x47b0038>
<>
标签: ruby-on-rails rails-activerecord has-and-belongs-to-many