我正在尝试利用ice_cube和recurring_select gem的强大功能来处理重复发生的事件。在我的数据库中有一个schedule (文本)列,在事件模型中有以下内容:
def schedule=(new_schedule)
write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_yaml)
end
def converted_schedule
Schedule.from_yaml(self.schedule, :start_date_override => self.start_date)
end 查看psql中的计划列,它似乎正确地存储了计划。
这是我的表格:
.control-group
= f.label 'Date', :class => 'control-label'
.controls
= f.text_field :start_date, :class => 'datepicker'
.control-group
= f.label 'Recurring?', :class => 'control-label'
.controls
= f.select_recurring :schedule, :allow_blank => true但是,当我尝试输出converted_schedule时,它只显示开始日期,而不显示任何其他日期。我有点怀疑我的修修补补没有成功...是否没有为converted_schedule方法正确地转换YAML?也许我需要一个结束日期(我不知道在recurring_select上哪里有这个功能)?
发布于 2013-06-14 03:24:59
在咨询了John Crepezzi ( ice_cube gem的作者--感谢John!)之后,我发现我存储的是递归规则,而不是时间表本身。以下代码修复了此问题:
serialize :schedule, Hash
def schedule=(new_schedule)
write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_hash)
end
def converted_schedule
the_schedule = Schedule.new(self.start_date)
the_schedule.add_recurrence_rule(RecurringSelect.dirty_hash_to_rule(self.schedule))
the_schedule
end注意:我还切换到将该列存储为散列,而不是之前发布的YAML。
https://stackoverflow.com/questions/17054670
复制相似问题