加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ice_cube和recurring_select宝石和事件

发布时间:2020-12-16 19:18:45 所属栏目:百科 来源:网络整理
导读:我正在尝试利用ice_cube和recurring_select宝石的[超棒]功能来处理重复发生的事件.我的数据库中有一个schedule(text)列,事件模型中有以下内容: def schedule=(new_schedule) write_attribute(:schedule,RecurringSelect.dirty_hash_to_rule(new_schedule).t
我正在尝试利用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读