Rails_Admin Multiple User Models + Development
So, I installed Devise and rails_admin in my current setup. I am currently trying to use rails for administration scripts.
Since I believed that many administrations required several "custom" models, I encountered difficulties in determining the correct way to design and organize their relationship.
So now I have a User (devise) model.
For user models, I decided to split the models that I need (Admin, Student (example), Professor (example)).
I read and found out that STI seems to be the solution here, so I figured they would be
class User < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
end
class Student < User
end
class Professor < User
end
In rails_admin, the CRUD user is mostly done and with this setting I was able to find the configuration I needed. For example, student creation will be recognized by the user. However, I am having trouble whether I have the correct setup or not, as I might actually need the Student to have their own attributes (like student_id or major, just throwing things here). Using the previous setup I mentioned will only allow me to create them using the attributes of the user model, not the student itself.
I also read somewhere that I need to have a column type in User that can be filled by either a student or a professor, but I am having trouble combining all of these solutions.
Any pointers / suggestions on how I should proceed now? Much appreciated.
source to share
If you choose to use one-page inheritance:
-
The attribute is
type
used by Rails to achieve a matching model and automatically execute it for you. those. when you executeStudent.new
, the attribute istype
set to "Student" -
attributes of all inherited classes (Student, Professor, etc.) are stored in a table
users
. This means that the Student and the Professor will havemajor
,fees_schedule
etc. (Which usually don't apply to professors.)
Here's the documentation about Individual Table Inheritance
On the other hand, you can consider Polymorphic Associations , where each table is separate and related row by row:
class User < ActiveRecord::Base
belongs_to :member, polymorphic: true
...
end
class StudentMember < ActiveRecord::Base
has_one :user, as: :member
...
end
class ProfessorMember < ActiveRecord::Base
has_one :user, as: :member
...
end
Read more about Polymorphic Associations here
Polymorphic associations seem to be more appropriate in your case as there are probably many different attributes for students, teachers, administrators, etc., and it will look pretty messy if you dump all of them into a table users
.
source to share