【问题标题】:Validation date in specific range特定范围内的验证日期
【发布时间】:2016-04-20 07:52:13
【问题描述】:

您好,我有模型 bought_detailsentry_types

# Table name: bought_details
#
#  id            :integer          not null, primary key
#  bought_data   :date             not null
#  end_on        :date             not null
#  entry_type_id :integer
#  person_id     :integer
#  start_on      :date
#  cost          :decimal(5, 2)    not null

# Table name: entry_types
#
#  id           :integer          not null, primary key
#  kind         :string           not null
#  kind_details :string           not null
#  description  :text
#  price        :decimal(5, 2)    not null

我想拥有什么:

如果我注册新购买,例如id 1 的人购买通行证(entry_type id:1)1 个月(start_on:20.04.2016 end_on:20.05.2016)。

当 start_on 或 end_on 在 20.04.2016 和 20.05. 2016 年。只需提醒“您已经拥有有效的通行证”。

如何解决我的问题? 提前致谢。

【问题讨论】:

  • 请显示到目前为止您尝试了什么。
  • 你可以在你的模型上定义一个验证函数,比如this
  • 约会之后的r.是什么?
  • @Stefan 这是一年的波兰语翻译。

标签: ruby-on-rails ruby validation


【解决方案1】:

你试过validates_timeliness gem 吗? https://github.com/adzap/validates_timeliness

它可以简单地验证日期范围。

validates_date :start_on, after: lambda { (DateTime.now - 1.month }, before: lambda { (DateTime.now + 1.month }

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多