尝试在 before_validation 中设置变量但它不起作用
因此,在我看来,我使用日历来选择日期,并使用下拉菜单来选择时间。因此,我使用 before_validation
方法将其组合在一起:
propose_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
当我创建对象时它工作得很好,但当我更新时却不行它。
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 *****************
但是当我使用它进行更新时,它会完全丢失并恢复到原来的状态。这让我感觉它实际上没有被保存。因此,为了帮助解释下一个的上下文,我有一个 ProposeTime
对象,它是 Consultation
的子对象(每次咨询有 3 个建议时间),它也有 Accepts_nested_attributes_for :propose_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
我认为它可能在 update_attributes 上引发了错误,但似乎并非如此。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有完全理解这一点,但我只想做一些简单的事情 - 我认为您希望starting_at不是局部变量,而是实际设置对象的starting_at属性:
关键位是
self。< /code>,实际上确保设置了属性,而不是仅存在于该方法中的同名局部变量。
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:
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.