Reputation: 539
I have a rails application with User and Machine data models, as well as several Test models named Test1, Test2, Test3, etc.
Users have many Machines
Machines belong to Users
Machines have many Tests
Tests belong to Machines
I want a user to be able to create a template for a machine report that includes any set of available tests.
For example, I want a user to be able to create a template called "My Machine Report" that includes only tests 1, 3, 4, 7, etc. The Machine>Show page would then only show the children tests numbered 1, 3, 4, 7, etc.
Does anyone have a suggestion for how I can create a data model that manages this? I am thinking of creating a new model called Templates that would include boolean values for each available test. Then I could display the proper input fields and results in a Show page based on which values are set to True in the Templates model.
Is this a good way to reach my goal?
Upvotes: 0
Views: 95
Reputation: 3158
You're on track with the Template model, but you don't want to have a different column on the templates table for each Test. What you've described is a many-to-many relationship between templates and tests which is handled by rails has_many :through association. My guess is your models will look something like this:
class Template < ActiveRecord::Base
belongs_to :user
belongs_to :machine
has_many :testing_templates
has_many :tests, :through => :testing_templates
end
and
class TestingTemplate < ActiveRecord::Base
belongs_to :template
belongs_to :test
end
and
class Test < ActiveRecord::Base
has_many :testing_templates
has_many :templates, :through => :testing_templates
end
Upvotes: 1