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.
model_run.rb
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
parameter_value.rb
class ParameterValue < ActiveRecord::Base
belongs_to :model_run
attr_accessible :type,
:parameter_id,
:description,
:numeric_value,
:model_run_id,
:parameter_file
end
numeric_parameter.rb
class NumericParameter < ParameterValue
attr_accessible :numeric_value
end
file_parameter.rb
class FileParameter < ParameterValue
mount_uploader :parameter_file, ParameterFileUploader
attr_accessible :parameter_file
end
parameter_file_uploader.rb
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