Reputation: 628
The problem:
People may have many types of degrees (bachelor, master, phd, etc...). In my country, some degrees includes another.
Ex.: Superior Degree may be either bachelor or licentiate.
I'd like to create this structure in a class hierarchy:
Bachelor < Superior < Degree
Licentiate < Superior < Degree
Master < Degree
PHD < Degree
Those classes don't need to have rows stored in the Database, but I'd like to associate them with some ActiveRecord objects of other classes.
Should I just store classes name as strings?
Upvotes: 0
Views: 1948
Reputation: 56
ActiveRecord
associations work between instances of classes, not classes themselves. If you simply need a marker pointing to a certain type of degree, you could use an ActiveRecord::Enum
property on the ActiveRecord
s having degrees. This would allow you to select records by their degrees.
That said, I would say your requirements easily justify a full ActiveRecord model. Use single table inheritance for the different degree types or even just one degree class, with a 'name' or 'type' property set to 'bachelor' and so on. Then one table row per degree type, having has_many
(or habtm
if it's n:m) associations to itself for the required degrees. The degrees would then be associated using regular rails associations (or polymorphic if you go with the STI variant).
If you don't want a table at all, you could also use ActiveModel::Associations
, but then you would have to create the different degree instances from code, which is cumbersome.
Upvotes: 1