I want to create school management app so that's why i have created four different models are Admin,Principal,Teacher,student because they have different information so that's why i created them , so how can i create them via single "devise gem" please clear my concept about how to associate them, i have tried to inherit them from user with STI but it only save the user attributes on student model and cant achieve what i want to do, there are other models too but they are associated with these four models and i am stuck at this point only ,so how can i do it any advise and idea . thanks in advance.
CodePudding user response:
Typically, site users such as Admins, Principals, Teachers and Students have lots of overlapping functionality, and the only true difference between each model is the privileges they have on the site. E.g. an admin might have full access, a teacher will be able to review tests and students have limited access only.
Because most functionality is shared, it would be nice if they have a common model, lets call this User
and generate that with Device:
rails generate devise User
Afterwards (or, if you prefer, in the create statement), we add a column role
to the User
database:
class AddRoleToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :role, :string, null: false, default: "pending"
end
end
Thanks to this role, we can limit certain portions of the site, or certain functionality, from the user. On top of that, we don't have to write the same code multiple times!
You could e.g. check a certain role like this:
class User < ApplicationRecord
ROLES = ["pending","admin","principal","teacher","student"]
def admin?(role)
self.role == "admin"
end
end
More approaches are possible, e.g. a binary role , or even another table Roles
that has a many to many relation with Users
, but the example is just to get you started.