samedi 28 mai 2016

Ruby on Rails, Enum user conditions from a different table

I have enums declared as follows in a table separate from my main users table (User.rb). I sign up users and give them a role from this 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 I have to define each role here but I tried it.

I was using a boolean method to separate the users before but switched to enum. I used to use this method type to restrict views based on role:

...unless current_user.teacher?...

This worked fine but now I have the enums declared in a different model to the users table it does not work.

A user has a role through the relationship between user.rb and school_user.rb. This works. I'm just looking for a way to set access based on user role/type in the views as above.

I hope not but I presume I will have to change all the conditions throughout my application?

I tried:

...unless current_user.school_user.teacher?...

and other variations.

Would appreciate any help. Thanks.

Aucun commentaire:

Enregistrer un commentaire