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"}
You are pretty close as far as your User model goes:
I’ll leave it up to you to figure out the best way to decide when
all_datesgets saved. In the example I’ve given,all_datesis used to update the User’s prices after the User is saved (during either update or create), and only whenall_datesis 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 thecreatemethod of your UserPrices, but I don’t see any problems in the code itself.