尝试在before_validation中设置变量,但是它不起作用 [英] Trying to set a variable in before_validation but it isn't working

查看:163
本文介绍了尝试在before_validation中设置变量,但是它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我看来,我正在使用日历选择日期,并使用下拉菜单选择时间.因此,我正在使用before_validation方法将其放在一起:

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.

日志

************** 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:

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

日志

...
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上引发了一个错误,但似乎并非如此.有什么想法吗?

I thought it might have been throwing an error on the update_attributes, but it doesn't seem like it is. Any ideas?

推荐答案

我还没有完全理解这一点,但我只是想简单讲一下-我想您希望starting_at不是局部变量,但实际上设置对象的starting_at属性:

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}")

关键位是self.,它实际上是确保设置了属性,而不是仅存在于该方法中的同名局部变量.

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.

这篇关于尝试在before_validation中设置变量,但是它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆