I'm facing a problem where I cannot permanently decide which columns one of my models will have. A use case will be this:
An admin creates a new dataset, he wants users to answer. In the dataset the admin defines several data points of different format and units.
I could imagine the classes to look similar to this:
class Dataset < ActiveRecord::Base
has_many :measurements
has_many :profiles, :through => :measurements
has_many :datapoints, :through => :dataset_datapoint
end
# Join table
class Dataset_datapoint < ActiveRecord::Base
belongs_to :dataset
belongs_to :datapoint
end
class Datapoint < ActiveRecord::Base
has_many :dataset, :through => :dataset_datapoint
has_many :data
# create_table "datapoints" do |t|
# t.string :name
# t.string :format # e.g. string, decimal etc.
# t.string :unit # e.g. CM, pounds etc.
end
class Data < ActiveRecord::Base
belongs_to :datapoint
# create_table "data" do |t|
# t.integer :datapoint_id
# t.string :value # This column could be anything from string to decimal
end
In my head, this seems pretty dynamic, but still quite easy to implement. What I'm worried about, is how to do the validation on every Data model that is created? Since I cannot hardcode the validation in the model? And to make it even more complicated, what if some datapoints require extra validations, such as minimum and maximum value?
Thanks in advance, Jonas