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

  • Home
  • SEARCH
  • 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 6702511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:03:45+00:00 2026-05-26T07:03:45+00:00

In my application I am trying to make a virtual attribute that’s a date_select

  • 0

In my application I am trying to make a virtual attribute that’s a date_select in my User model that associates itself to my UserPrice model’s date_select. The reason being, I am trying to have only one date dropdown list that users have to select. I plan on doing this by making the User virtual date_select update all of my UserPrice date_selects to that particular date.

I have my UserPrice model with the :purchase_date attribute:

create_table :user_prices do |t|
      t.decimal :price
      t.date :purchase_date

I have a nested form which can be shown here by looking at my UserPrice controller which also on create updates all of the UserPrices generated on the form:

UserPrices Controller

  def add_store_prices
     @a_new_user = User.new
     5.times do
        @a_new_user.user_prices.build
     end
  end

  def create
    respond_to do |format|
      if current_user.update_attributes(params[:user])
        redirect_to :back
      else
        redirect_to :back
      end
    end
  end

Then I am trying to define in the User model how to update the attributes of the :purchase_dates and also get the f.date_select field to work:

class User < ActiveRecord::Base
attr_accessible :user_prices_attributes, :all_dates
has_many :user_prices
attr_writer :all_dates

def all_dates
# @all_dates  = UserPrice :purchase_date field?
    #Needs to update the :purchase_date of the UserPrices
end

I am not exactly sure where to start. How do I define the virtual attribute :all_dates and get it to update on create all of the UserPrice’s :purchase_date attributes generated on my form?

The end result should look something like this:

<%= form_for(@a_new_user, :url => {:controller => :user_prices, :action => :create}) do |f| %>
 <%= f.error_messages %>
  <%= f.date_select :all_dates  %>
    <%= f.fields_for :user_prices do |pf| %>
      <%= pf.text_field :product_name %>
      <%= pf.text_field :store %>
      <%= pf.text_field :price %>
    <% end %>
 <%= f.submit %>
<% end %>

Thanks, really need some help with this.

EDIT

This is the error I get with the following controller code:

attr_accessor :all_dates
  after_save    :save_all_dates_to_user_prices

  protected

  def save_all_dates_to_user_prices 
    if !self.all_dates.nil?       
      self.user_prices.update_all :purchase_date => self.all_dates
    end
  end


ActiveRecord::MultiparameterAssignmentErrors in UserPricesController#create

1 error(s) on assignment of multiparameter attributes

app/controllers/user_prices_controller.rb:30:in `block in create'
app/controllers/user_prices_controller.rb:29:in `create'

"utf8"=>"✓",
 "authenticity_token"=>"fmB0kymP2vetFDoI/BB6RkyMEgsnhvf04cJ5Vu1GEaI=",
 "user"=>{"all_dates(2i)"=>"10",
 "all_dates(3i)"=>"17",
 "all_dates(1i)"=>"2011",
 "user_prices_attributes"=>{"0"=>{"product_name"=>"Cherries",
 "store"=>"4255 Alafaya Trail,
 Oviedo,
 FL 32765,
 USA",
 "price"=>"2.99"},
 "1"=>{"product_name"=>"",
 "store"=>"",
 "price"=>"5.99"},
 "2"=>{"product_name"=>"",
 "store"=>"",
 "price"=>""},
 "3"=>{"product_name"=>"",
 "store"=>"",
 "price"=>""},
 "4"=>{"product_name"=>"",
 "store"=>"",
 "price"=>""}}},
 "commit"=>"Done"}
  • 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-26T07:03:45+00:00Added an answer on May 26, 2026 at 7:03 am

    You are pretty close as far as your User model goes:

    class User < ActiveRecord::Base
    
      # See: http://gabeodess.heroku.com/posts/14
      composed_of :all_dates, :class_name => "DateTime",
        :mapping => %w(Time to_s),
        :constructor => Proc.new { |item| item },
        :converter => Proc.new { |item| item }
    
      after_save    :save_all_dates_to_user_prices
    
      protected
    
      def save_all_dates_to_user_prices 
        if !self.all_dates.nil?       
          self.user_prices.update_all :purchase_date => self.all_dates
        end
      end
    
    end
    

    I’ll leave it up to you to figure out the best way to decide when all_dates gets saved. In the example I’ve given, all_dates is used to update the User’s prices after the User is saved (during either update or create), and only when all_dates is not nil (for example, whenever an input is provided on the form).

    I assume that you’ve set up your User model so that it accepts_nested_attributes_for :user_prices. I don’t quite understand why you are updating the current user in the create method of your UserPrices, but I don’t see any problems in the code itself.

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

Sidebar

Related Questions

I'm trying to make an application that keeps an object model in sync with
im trying to make an android application that whenever a user conncects to a
I'm trying to make a little console application that is able to deal with
i trying to make a osx application that just have one window and it
I'm trying to make an application that can store a tree structure with files
I'm trying to make an application (or webservice) hosted on IIS 6 that would
Im trying to make a very simple application that lets my client create their
Hi am trying to make an application that post data to a joomla login
I'm trying to make an application that needs to draw on the desktop, behind
On my application I am trying to make it so that it opens a

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.