我正在寻找一种 Rails-y 方法来处理以下问题:
事件模型中的两个datetime
属性:
start_at: datetime
end_at: datetime
我想使用 3 个字段以表单形式访问它们:
event_date
start_time
end_time
我遇到的问题是如何使实际和虚拟属性保持“同步”,以便可以通过表单和/或直接通过start_at
&更新模型end_at
。
class Event < ActiveRecord::Base
attr_accessible :end_at, :start_at, :start_time, :end_time, :event_date
attr_accessor :start_time, :end_time, :event_date
after_initialize :get_datetimes # convert db format into accessors
before_validation :set_datetimes # convert accessors into db format
def get_datetimes
if start_at && end_at
self.event_date ||= start_at.to_date.to_s(:db) # yyyy-mm-dd
self.start_time ||= "#{'%02d' % start_at.hour}:#{'%02d' % start_at.min}"
self.end_time ||= "#{'%02d' % end_at.hour}:#{'%02d' % end_at.min}"
end
end
def set_datetimes
self.start_at = "#{event_date} #{start_time}:00"
self.end_at = "#{event_date} #{end_time}:00"
end
end
哪个有效:
1.9.3p194 :004 > e = Event.create(event_date: "2012-08-29", start_time: "18:00", end_time: "21:00")
=> #<Event id: 3, start_at: "2012-08-30 01:00:00", end_at: "2012-08-30 04:00:00", created_at: "2012-08-22 19:51:53", updated_at: "2012-08-22 19:51:53">
直到直接设置实际属性(end_at
设置回end_time
验证):
1.9.3p194 :006 > e.end_at = "2012-08-30 06:00:00 UTC +00:00"
=> "2012-08-30 06:00:00 UTC +00:00"
1.9.3p194 :007 > e
=> #<Event id: 3, start_at: "2012-08-30 01:00:00", end_at: "2012-08-30 06:00:00", created_at: "2012-08-22 19:51:53", updated_at: "2012-08-22 19:51:53">
1.9.3p194 :008 > e.save
(0.1ms) BEGIN
(0.4ms) UPDATE "events" SET "end_at" = '2012-08-30 04:00:00.000000', "start_at" = '2012-08-30 01:00:00.000000', "updated_at" = '2012-08-22 20:02:15.554913' WHERE "events"."id" = 3
(2.5ms) COMMIT
=> true
1.9.3p194 :009 > e
=> #<Event id: 3, start_at: "2012-08-30 01:00:00", end_at: "2012-08-30 04:00:00", created_at: "2012-08-22 19:51:53", updated_at: "2012-08-22 20:02:15">
1.9.3p194 :010 >
我的假设是我还需要自定义“实际”属性的设置器,但我不确定如何在不破坏默认行为的情况下做到这一点。想法?也许有更多的“Rails-y”“回调-y”方式来处理这个?