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 6765909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:46:05+00:00 2026-05-26T14:46:05+00:00

I have a model named UserPrice which has the attribute :purchase_date (a date_select) in

  • 0

I have a model named UserPrice which has the attribute :purchase_date(a date_select) in its table. With my form I can create multiple user_prices at once but for user convenience I made a virtual attribute inside of my UserPrice model called :all_dates that’s also a date_select field and its job is to be the replacement of the :purchase_dates so users only have to select the :all_dates field for the date.


Problem & Question

The :all_dates field is not updating the :purchase_date fields of my user_prices that are being created. What do I need to do in order to get my :all_dates field to update the :purchase_date fields of my new UserPrices?

Does anyone have any tips on how to do this?


Parameters

Parameters: 
"user_price"=> { 
"all_dates(2i)"=>"10", 
"all_dates(3i)"=>"27", 
"all_dates(1i)"=>"2011"
}, 
"user_prices"=>
{
"0"=>{"product_name"=>"Item1", "store"=>"Apple Store","price"=>"6"}, 
"1"=>{"product_name"=>"Item2", "store"=>"Apple Store", "price"=>"7"}
}, 
"commit"=>"Submit"}

Code

  class CreateUserPrices < ActiveRecord::Migration
    def self.up
       create_table :user_prices do |t|
          t.decimal :price
          t.integer :product_id
          t.date :purchase_date
          t.timestamps
        end
     end
  end

I took out the :purchase_date field so it isn’t inside of the user_price loop.

<%= form_tag create_multiple_user_prices_path, :method => :post do %>
 <%= date_select("user_price", "all_dates" )  %>
   <% @user_prices.each_with_index do |user_price, index| %>
      <%= fields_for "user_prices[#{index}]", user_price do |up| %>
          <%= render "user_price_fields", :f => up %>
      <% end %>
   <% end %>
<% end %>




class UserPrice < ActiveRecord::Base
  attr_accessible :price, :product_name, :purchase_date, :all_dates, :store
  attr_accessor :all_dates
  after_save :save_all_dates_to_user_prices
  composed_of :all_dates, :class_name => "DateTime",
    :mapping => %w(Time to_s),
    :constructor => Proc.new { |item| item },
    :converter => Proc.new { |item| item }

  def user_prices
    @user_prices = Array.new() { UserPrice.new }
  end

  protected

  def save_all_dates_to_user_prices 
     if !self.all_dates.nil?       
      self.user_prices.each {|up| up.purchase_date = self.all_dates if up.new_record?}
     end
  end


class UserPricesController < ApplicationController

 def new
    @user_prices = Array.new(5) { UserPrice.new }
 end

 def create_multiple
   @user_prices = params[:user_prices].values.collect { |up| UserPrice.new(up) }
   if @user_prices.all?(&:valid?)
     @user_prices.each(&:save!)
     redirect_to :back, :notice => "Successfully added prices."
   else
     redirect_to :back, :notice => "Error, please try again."
   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-05-26T14:46:05+00:00Added an answer on May 26, 2026 at 2:46 pm

    This is a case of trying to do in a model what is better left to the controller. All you’re trying to do here is to auto-assign a certain attribute on creation from a parameter not directly tied to your model. But you’re not even passing that extra parameter to the model anywhere – you’re creating your model instances from the user_prices parts of the parameter hash, but the user_price sub-hash is not used anywhere. In any case, this is behavior that is more closely related to the view and action taken than the model, so keep it in the controller.

    Try this:

    1. Throw out the virtual attribute, and get rid of the whole after_save callback stuff
    2. Throw away the user_prices method in your model
    3. Change the all_dates attribute name back to purchase_date in the form

    Then your parameter hash should look like this:

    {"user_price"=> { 
      "purchase_date(2i)"=>"10", 
      "purchase_date(3i)"=>"27", 
      "purchase_date(1i)"=>"2011"
    }, 
    "user_prices"=>
    {
      "0"=>{"product_name"=>"Item1", "store"=>"Apple Store","price"=>"6"}, 
      "1"=>{"product_name"=>"Item2", "store"=>"Apple Store", "price"=>"7"}
    }}
    

    All that’s left to do is to merge the single user_price attributeS into each user_prices sub-hash in your create_multiple action. Replace the first line in that action with this:

    @user_prices = params[:user_prices].values.collect do |attributes| 
      UserPrice.new(attributes.merge(params[:user_price])) 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model named UserPrice which has a form where you can create
I have a model named 'Object' which loads the 'Objects' table I have a
Hello I have a model named Client which has nested models called Receiver and
I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner
I have a relatively simple problem. I have a model named Item which I've
sorry for my english, I have a model named Recipe and Recipe has an
I have a Rails 3.0.0 application that has a model named 'Encoding' and is
I have a model named Domain which looks like this: class Domain(models.Model): Model for
I have a model named Person. It has two properties - name and parent_person_id
I have a model named Post. I want to use a modal form to

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.