Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:12:40+00:00 2026-06-01T14:12:40+00:00

I have a model, ModelRun , that accepts nested attributes for another model, ParameterValue

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:12:42+00:00Added an answer on June 1, 2026 at 2:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model with ManyToManyField field. I also have ModelForm that uses this model.
I have model Article it has field title with some text that may contain
In my controller I have model operations that can return empty results. I've setup
I have a situation where I have Model A that has a variety of
So I have model with an association and I'm using a single form with
I have model represent association rule (Body => Head) def Item has_many :heads has_many
I have model that is a queue of Strings associated with enum types. I'm
I have model called User with a has_many relationship to UserFilter . UserFilter has
I have model that looks like this: public class Lesson { public int Id
I have model data that came from a MultipleChoiceField. I have figured out it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.