Lai Yu-Hsuan
Lai Yu-Hsuan

Reputation: 28081

Rails: Deal with several similar model classes?

I'm building a course application system. The high school, undergraduate and graduate students are all able to apply for this course. They have to fill out some application form.

However, their information forms are similar, but not exactly the same. Every student has name, phone number, email, address, etc. But only undergraduate students have to provide their GPA, and graduate students is required to tell which lab they are researching at. There are other subtle differences...

So how should I deal with this? Make a big table, but leave 'GPA' column of high school students NULL? Or use three separate tables?

Moreover, there are some relation between Student(or, in three tables case, HighSchoolStudent, UndergraduateStudent and GraduateStudent) and other models. For instance, Course has many Students, Student has many Questions, and so on.

Upvotes: 0

Views: 301

Answers (2)

Harish Shetty
Harish Shetty

Reputation: 64363

You can use a combination of STI and Store feature achieve this.

Declare a base model for Student with a text column called settings.

class Student < ActiveRecord::Base
  store :settings
  # name, email, phone, address etc..
end


class HighSchoolStudent < Student
  # declare HighSchoolStudent specific attributes 
  store_accessor :settings, :gpa
end


class UndergraduateStudent < Student
  # declare UndergraduateStudent specific attributes 
  store_accessor :settings, :attr1
end

class GraduateStudent< Student
  # declare GraduateStudent specific attributes 
  store_accessor :settings, :attr2
end

In the sample above, instances of HighSchoolStudent will have an attribute called gpa.

Upvotes: 1

agmcleod
agmcleod

Reputation: 13621

You could go with the option you thought of like leaving GPA null, and set up custom validations for the model so it only checks depending on the student type. Single table inheritance is also an option, where you specify the different class names to use in a column on the database table, and then you simply add these classes in the model directory. You can see some documentation on it here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

I haven't tried STI before, but given what you stated above, I'd probably go for that route, branch off my code and would see how it pans out.

Upvotes: 0

Related Questions