问题
So in my view I'm using a calendar to select a day and drop downs to select time. Therefore I'm using a before_validation
method to put it together:
proposed_time.rb
before_validation :construct_starting_at
def construct_starting_at
d = Time.parse(date)
puts "************** construct_starting_at BEGIN *****************"
puts "DATE: #{d}"
puts "Time: #{time}"
puts "Timezone: #{timezone}"
puts "construct_starting_at :: #{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 #{timezone}"
if date.present? && time.present? && timezone.present?
starting_at = Time.zone.parse("#{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 #{timezone}")
end
puts "starting_at: #{starting_at}"
puts "************** construct_starting_at END *****************"
end
And it works just fine when I'm creating an object, but not when I'm updating it.
log
************** construct_starting_at BEGIN *****************
DATE: Fri Jun 03 00:00:00 -0500 2011
Time: Thu May 19 23:00:00 UTC 2011
Timezone: (GMT-05:00) Eastern Time (US & Canada)
construct_starting_at :: 2011-6-3 23:0:00 (GMT-05:00) Eastern Time (US & Canada)
starting_at: 2011-06-04 00:00:00 -0400
************** construct_starting_at END *****************
But when I use it for an update it looses it completely and reverts to what it was. This makes me feel like it isn't actually being saved. So to help explain context on this next one, I have a ProposedTime
object and it is a child of Consultation
(each consultation has 3 proposed times) which also has accepts_nested_attributes_for :proposed_times
:
consultation.rb
def proposed_times_attributes=(attributes)
puts "$$$$$$$$$$$$$$ proposed_times_attributes $$$$$$$$$$$$$$$$"
attributes.each do |key,value|
value[:timezone] = timezone
if value[:id]
puts "Updating #{value[:id]}"
p = ProposedTime.find(value[:id])
value.delete(:id)
unless p.update_attributes(value)
puts "@@@@@@@@@@@@@@@@@ ERROR @@@@@@@@@@@@@@@"
error.add(:proposed_times, "something is wrong")
end
puts "-- starting_at: #{p.starting_at}"
else
puts "Creating a new proposed time"
proposed_times << ProposedTime.new(value)
end
end
puts "$$$$$$$$$$$$$$ proposed_times_attributes $$$$$$$$$$$$$$$$"
end
log
...
Updating 18
************** construct_starting_at BEGIN *****************
DATE: Fri Jun 03 00:00:00 -0500 2011
Time: Thu May 19 23:00:00 UTC 2011
Timezone: (GMT-05:00) Eastern Time (US & Canada)
construct_starting_at :: 2011-6-3 23:0:00 (GMT-05:00) Eastern Time (US & Canada)
starting_at: 2011-06-04 00:00:00 -0400
************** construct_starting_at END *****************
-- starting_at: 2011-06-01 06:00:00 -0400
I thought it might have been throwing an error on the update_attributes, but it doesn't seem like it is. Any ideas?
回答1:
I haven't groked this fully, but I'm just going for the simple things - I think you want starting_at to not be a local variable, but actually set your object's starting_at attribute:
self.starting_at = Time.zone.parse("#{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 #{timezone}")
The key bit being the self.
, to actually make sure the attribute is set, not some local variable of the same name that only exists within that method.
来源:https://stackoverflow.com/questions/6065860/trying-to-set-a-variable-in-before-validation-but-it-isnt-working