Ruby on Rails, Enum user conditions from a different table -
i have enums declared follows in table separate main users table (user.rb). sign users , give them role table:
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
i don't think have define each role here tried it.
i using boolean method separate users before switched enum. used use method type restrict views based on role:
...unless current_user.teacher?...
this worked fine have enums declared in different model users table not work.
a user has role through relationship between user.rb , school_user.rb. works. i'm looking way set access based on user role/type in views above.
i hope not presume have change conditions throughout application?
i tried:
...unless current_user.school_user.teacher?...
and other variations.
would appreciate help. thanks.
edit: 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
you can delegate methods :school_user
:
class user < activerecord::base delegate :school?, :student?, :parent?, :teacher?, to: :school_user
also, remove methods schooluser
class, mentioned in comments
Comments
Post a Comment