I have a model, ModelRun, that accepts nested attributes for another model, ParameterValue. (ModelRun has_many :parameter_values.) However, ParameterValue also employs single-table inheritance to save two subclasses: NumericParameter and FileParameter. FileParameter uses CarrierWave to store a file.
The problem is that in ModelRunController when saving or updating a ModelRun, by default, @model_run.save or @model_run.update_attributes does not identify the type of ParameterValue attributes - it just tries to store them as ParameterValue. This works for NumericParameter values, but it raises an exception for FileParameters because the CarrierWave uploader doesn't get mounted to handle the file upload so ActiveRecord fails when trying to serialize the file to the database.
What's the cleanest way to handle this problem? The only solution that occurred to me was to manually populate the @model_run.parameter_values collection in the controller's create and update methods, since I can tell which type each ParameterValue should be and create the correct objects one by one. However, this seems like reimplementing a lot of Rails magic since I can't just use ModelRun.new(params[:model_run]) or @model_run.update_attributes anymore - seems like it throws away much of the advantage of using accepts_nested_attributes_for in the first place. Is there a better way, a Rails Way™?
Relevant parts of each model are copied below.
class ModelRun < ActiveRecord::Base
has_many :parameter_values, dependent: :destroy
accepts_nested_attributes_for :parameter_values, allow_destroy: true
attr_accessible :name,
:description,
:geometry_description,
:run_date,
:run_date_as_string,
:parameter_values_attributes
end
class ParameterValue < ActiveRecord::Base
belongs_to :model_run
attr_accessible :type,
:parameter_id,
:description,
:numeric_value,
:model_run_id,
:parameter_file
end
class NumericParameter < ParameterValue
attr_accessible :numeric_value
end
class FileParameter < ParameterValue
mount_uploader :parameter_file, ParameterFileUploader
attr_accessible :parameter_file
end
class ParameterFileUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"#{Rails.root}/uploads/#{model.class.to_s.underscore}/#{model.id}"
end
def cache_dir
"#{Rails.root}/tmp/uploads/cache/#{model.id}"
end
end
If i understand you well, you are trying to find convinient way of instantiating right subclass in STI hierarchy by passing :type?. If you don't need to change the type later, you can just add this hack to your ParameterValue class
class ParameterValue < ActiveRecord::Base
class << self
def new_with_cast(*attributes, &block)
if (h = attributes.first).is_a?(Hash) && !h.nil? && (type = h[:type] || h['type']) && type.length > 0 && (klass = type.constantize) != self
raise "wtF hax!!" unless klass <= self
return klass.new(*attributes, &block)
end
new_without_cast(*attributes, &block)
end
alias_method_chain :new, :cast
end
end
After this, passing right type will cause right ParameterValue instatntiating, including uploaders, validation etc.