I have a model which uses Single table inheritance and has different types (school year of type either semester or quarter) and each type has its own validations. If a user tries to change the type of the record, he can select which academic year type it is from a drop down and make changes. however, if the user changes the type, i cannot figure out how to make new class validations run and not old validations. For instance, my code is as follows:
@school_year = SchoolYear.find(params[:id])
respond_to do |format|
if SchoolYear::SUBCLASSES.include? params[:school_year]['type']
@school_year[:type] = params[:school_year]['type']
else
raise "Invalid type"
end
if @school_year.update_attributes(params[:school_year])
# done
else
# validation problem?
end
now if the year's type was semester, and the user changes it to quarter, i expect the QuarterSchoolYear's validations to run and not of those of semester. How do i make changes to code to make it work?
I guess you should reload the object after you change the type. Just assigning a new value to the 'type' attribute will not change the ruby class of the object. Of course, when you save the object just after the type change, the old validations will be used.
You may try to update the type attribute in the database, and then load the object. Something like:
Be sure to have :type NOT in attr_accessible for this class.
Besides that, I find it a little disturbing to change the type of the object, but that may be just my personal fear. :)