I have a question about STI in rails that I cannot seem to get my head around.
I have 2 models, order.rb
class Order < ActiveRecord::Base
has_many :answers
end
and answer.rb
class Answer < ActiveRecord::Base
belongs_to :order
attr_accessible :value, :input_id, :type
end
answer.rb
also has a couple of STI classes like select.rb
class Select < Answer
validates presence: true
end
The reason for this is that I want to be able to make custom validations and caluclations on different types of answers.
Everything works ok until I'm trying to make a fields_for on answers that is associated to each order. I am abled to instiate each STI class when rendering the form. All of the answers will however end up in the array answers_attributes
when the form is submitted and will all be considered as class Answer
instead of for example class Select
.
Anyone got some idea on how I might solve this issue?
Thanks!