[英]ice_cube and recurring_select gems and occurrences
我正在嘗試利用ice_cube和recurring_select寶石的[超棒]功能來處理重復發生的事件。 我的數據庫中有一個schedule
(text)列,事件模型中有以下內容:
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中的schedule列,它似乎正確地存儲了日程表。
這是我的表格:
.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
,它只顯示開始日期,不會顯示任何其他日期。 我有一些懷疑,我沒有成功修改...也許YAML沒有正確轉換為converted_schedule
方法? 也許我需要一個結束日期(我沒有看到recurring_select上這個功能可用的地方)?
在咨詢了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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.