所以在我看来,我使用日历来选择一天并使用下拉菜单来选择时间。因此,我使用一种before_validation
方法将其组合在一起:
提议的时间.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
当我创建一个对象时它工作得很好,但当我更新它时就不行了。
日志
************** 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 *****************
但是当我将它用于更新时,它会完全失去它并恢复到原来的状态。这让我觉得它实际上并没有被拯救。因此,为了帮助解释下一个的上下文,我有一个ProposedTime
对象,它是Consultation
(每个咨询有 3 个建议的时间)的子对象,它也有accepts_nested_attributes_for :proposed_times
:
咨询.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
日志
...
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
我认为它可能在 update_attributes 上引发了错误,但似乎并非如此。有任何想法吗?