首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ice_cube和recurring_select的gem和实例

ice_cube和recurring_select的gem和实例
EN

Stack Overflow用户
提问于 2013-06-12 06:06:58
回答 1查看 2.3K关注 0票数 8

我正在尝试利用ice_cube和recurring_select gem的强大功能来处理重复发生的事件。在我的数据库中有一个schedule (文本)列,在事件模型中有以下内容:

代码语言:javascript
复制
  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中的计划列,它似乎正确地存储了计划。

这是我的表格:

代码语言:javascript
复制
.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上哪里有这个功能)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-14 03:24:59

在咨询了John Crepezzi ( ice_cube gem的作者--感谢John!)之后,我发现我存储的是递归规则,而不是时间表本身。以下代码修复了此问题:

代码语言:javascript
复制
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。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17054670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档