我在 Rails 中遇到了一个奇怪的问题。我想在 Rails 的模型中设置字段的值,但我做不到,即使在 Rails 控制台中也是如此。观察:
1.9.3p393 :008 > campy = Campaign.last
Campaign Load (0.8ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` DESC LIMIT 1
=> #<Campaign id: 3, name: "Stuff", ..., approved_at: nil, approval_requested_at: nil>
1.9.3p393 :009 > campy.approved_at = Time.now
=> 2013-07-01 00:54:38 +0200
1.9.3p393 :010 > campy.save
(0.2ms) BEGIN
(0.2ms) COMMIT
=> true
1.9.3p393 :011 > campy.approved_at
=> 2013-07-01 00:54:38 +0200
1.9.3p393 :012 > Campaign.last.approved_at
Campaign Load (0.7ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` DESC LIMIT 1
=> nil
1.9.3p393 :013 > campy = Campaign.last
Campaign Load (0.6ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` DESC LIMIT 1
=> #<Campaign id: 3, name: "Stuff", ..., approved_at: nil, approval_requested_at: nil>
1.9.3p393 :014 > campy.approved_at = Time.now
=> 2013-07-01 00:55:11 +0200
1.9.3p393 :015 > campy.save
(0.3ms) BEGIN
(0.2ms) COMMIT
=> true
1.9.3p393 :016 > Campaign.last.approved_at
Campaign Load (0.7ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` DESC LIMIT 1
=> nil
我正在尝试将最后一个活动的approved_at 属性的值设置为Time.now,但它仍然为零,即使在我保存它之后也是如此。可以通过 Campaign 模型顶部的 attr_accessor 函数获得approved_at。为什么我不能更改此属性的值?
编辑:
该问题的另一个有趣方面是,当我设置 campy.approved_at 然后显式查找该值时,我得到了正确的值。但是,当我将 campy 视为变量时,Rails 仍将值显示为 nil
1.9.3p393 :027 > campy.approved_at = Time.now
=> 2013-07-01 01:19:05 +0200
1.9.3p393 :028 > campy.save
(0.2ms) BEGIN
(0.2ms) COMMIT
=> true
1.9.3p393 :029 > campy.approved_at
=> 2013-07-01 01:19:05 +0200
1.9.3p393 :030 > campy
=> #<Campaign id: 3, name: "Stuff", ... , approved_at: nil, approval_requested_at: nil>