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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:28:59+00:00 2026-05-26T13:28:59+00:00

I am using a virtual attribute called :all_dates on my form . The point

  • 0

I am using a virtual attribute called :all_dates on my form . The point of this field is to replace the :purchase_date attribute of my UserPrice model with the date of my :all_dates field. The reason for this is so user’s don’t have to change the :purchase_date of all of the user_price records they want to create on the form (they can create a maximum of 5), so what it suppose to do is update the columns of the user_prices with the date that is given from the :all_dates field.


Problem

Unfortunately on creating 1 to 5 records of user_prices, I get a NoMethodError because of the :all_dates field:

NoMethodError (undefined method `user_prices' for #<UserPrice:0x485d918>):
  app/models/user_price.rb:54:in `save_all_dates_to_user_prices'
  app/controllers/user_prices_controller.rb:27:in `each'
  app/controllers/user_prices_controller.rb:27:in `create_multiple'

UPDATE

I got rid of the NoMethodError by putting this in my UserPrice model:

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

But that isn’t correct because the :all_dates field doesn’t update my UserPrice :purchase_date fields. Does anyone have any ideas?


Question

How do I define the method user_prices?
I am guessing its suppose to be able to loop several new records of UserPrice but how is that done?


Code

This form acts like a nested form but instead of using two or more models its just using one single model which is my UserPrice to generate more records on the form, in my case being 5 new ones.

<%= 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 "add_store_price_fields", :f => up %>
      <% end %>
   <% end %>
<% end %>

  class UserPrice < ActiveRecord::Base
  attr_accessible :price, :product_name, :all_dates
  attr_accessor :all_dates
  after_save :save_all_dates_to_user_prices

  protected

  def save_all_dates_to_user_prices
      self.user_prices.each {|up| up.purchase_date = self.all_dates if up.new_record?}
  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-26T13:28:59+00:00Added an answer on May 26, 2026 at 1:28 pm

    I Took a totally different approach and was still able to get the same results in this Question: How to update a model's attribute with a virtual attribute?

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

Sidebar

Related Questions

I want to implement my own clustering algorithm using this Virtual Earth javascript API:
I have this code to show a map using the Virtual Earth API: <script
I have attribute name of model Person. I want to use html-form with fields:
I am using virtual attributes to save tags in an Entry model from a
I'm having an odd error with a virtual attribute in a form helper. My
My problem is that iam using admin generator for model with some extra virtual
I am using virtual machines for development,but each time I need a new VM,
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
I'm working on a graphics application that is using virtual classes fairly extensively. It
What is the best breakdown of responsibility when using a virtual grid and the

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.