【发布时间】:2016-05-28 18:38:39
【问题描述】:
我在与主用户表 (User.rb) 不同的表中声明了如下枚举。我注册用户并为他们分配此表中的角色:
school_user.rb
class SchoolUser < ActiveRecord::Base
belongs_to :user
belongs_to :school
enum user_type: [:school, :student, :parent, :teacher]
def school?
end
def teacher?
end
def student?
end
def parent?
end
end
我认为我不必在这里定义每个角色,但我尝试过。
我之前使用布尔方法来分隔用户,但切换到枚举。我曾经使用这种方法类型来根据角色限制视图:
...unless current_user.teacher?...
这工作得很好,但现在我在与用户表不同的模型中声明了枚举,它不起作用。
用户通过 user.rb 和 school_user.rb 之间的关系拥有一个角色。这行得通。我只是在寻找一种在上述视图中根据用户角色/类型设置访问权限的方法。
我希望不会,但我认为我必须在整个申请过程中更改所有条件?
我试过了:
...unless current_user.school_user.teacher?...
和其他变体。
不胜感激。谢谢。
编辑:User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence:true,
length: {minimum: 4 }
validates :last_name, presence:true
has_many :messages, foreign_key: :sender_id
has_many :notes
has_one :school_user
accepts_nested_attributes_for :school_user
【问题讨论】:
-
你能发布你的用户模型吗?
-
@Pavan - 我编辑了问题 - 包括 user.rb
-
请阅读“minimal reproducible example”。提供演示问题的最少代码示例比尝试描述它需要我们想象您的代码要好得多。如果没有详细信息,您的问题就很宽泛。
-
还可以尝试在
school_user模型中删除这些方法(school?、teacher?等) -
试过了,但是得到了老师的未定义方法?,学校?等等……
标签: ruby-on-rails ruby ruby-on-rails-3 enums