【问题标题】:Trying to set a variable in before_validation but it isn't working试图在 before_validation 中设置一个变量,但它不起作用
【发布时间】:2011-05-19 23:13:01
【问题描述】:

所以在我看来,我使用日历来选择一天并使用下拉菜单来选择时间。因此,我使用before_validation 方法将其组合在一起:

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 

当我创建一个对象时它工作得很好,但当我更新它时就不行了。

日志

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

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 update-attributes


    【解决方案1】:

    我还没有完全理解这一点,但我只是在做一些简单的事情——我认为你希望starting_at 不是一个局部变量,而是实际设置你的对象的starting_at 属性:

    self.starting_at = Time.zone.parse("#{d.year}-#{d.month}-#{d.day} #{time.hour}:#{time.min}:00 #{timezone}")
    

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

    【讨论】:

    • 知道什么时候使用self.,什么时候不需要?还是一直使用它只是最佳实践?感谢您的回答!
    • 在设置实例/对象变量时使用它。如果您正在调用方法或获取实例/对象变量的值,则不需要 self。
    • 感谢您的快速回复。很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多