Rails:验证大于另一个的日期

时间:2012-05-18 15:36:17

标签: ruby-on-rails ruby validation date

我在ActiveRecord中编写自定义验证器,以便截止日期有意义:

  validate :deadline_is_possible?

  def deadline_is_possible?
    if deadline > begins_at
      errors.add(:deadline, 'must be possible')
    end
  end

然而,这会产生一个" NoMethodError:未定义的方法`>'为零:NilClass"。我的事件试图将日期变成字符串,例如:

  def deadline_is_possible?
    if deadline.to_s > begins_at.to_s
      errors.add(:deadline, 'must be possible')
    end
  end

虽然它没有产生错误,但也无法正常工作。

我还宣布了其他验证器(例如

  def begins_at_is_date?
    if !begins_at.is_a?(Date)
      errors.add(:begins_at, 'must be a date')
    end
  end

工作正常。

3 个答案:

答案 0 :(得分:4)

如果其中一个日期为零,您可能需要处理。您可以将这些设置为在截止日期数据库中具有默认值,或执行以下操作:

  validate :deadline_is_possible?

  def deadline_is_possible?
    return if [deadline.blank?, begins_at.blank?].any?
    if deadline > begins_at
      errors.add(:deadline, 'must be possible')
    end
  end

答案 1 :(得分:1)

抱怨是因为deadline是零。打印它以确认这是问题。

irb(main):001:0> nil > Date.new
NoMethodError: undefined method `>' for nil:NilClass

答案 2 :(得分:0)

您可以添加自己的验证器:

# -*- coding: utf-8 -*-
#Created on Fri Jul 12 15:50:54 2019

#@author: CGDELL23

import tkinter as tk
import cx_Oracle

class Application(tk.Frame):
    def __init__(self,master=None):
        tk.Frame.__init__(self,master)
        self.grid() 
        self.createWidgets()

    def createWidgets(self):

        #Employee Details
        self.eNameLabel = tk.Label(self,text='Employee Name')
        self.eNameValue = tk.Entry(self)
        self.eNameLabel.grid(row=0,column=0) 
        self.eNameValue.grid(row=0,column=1)

        self.eIdLabel = tk.Label(self, text='Employee Id')
        self.eIdValue = tk.Entry(self)
        self.eIdLabel.grid(row=1,column=0) 
        self.eIdValue.grid(row=1,column=1)

        self.eSalaryLabel = tk.Label(self, text='Employee Salary')
        self.eSalaryValue = tk.Entry(self)
        self.eSalaryLabel.grid(row=2,column=0) 
        self.eSalaryValue.grid(row=2,column=1)

        #CRUD Buttons
        self.CreateButton = tk.Button(self,text='Create', command=self.Create)
        self.CreateButton.grid(row=3,column=0)

        self.ReadButton = tk.Button(self,text='Read', command=self.Read)
        self.ReadButton.grid(row=3,column=1)

        self.UpdateButton = tk.Button(self,text='Update', command=self.Update)
        self.UpdateButton.grid(row=3,column=2)

        self.DeleteButton = tk.Button(self,text='Delete', command=self.Delete)
        self.DeleteButton.grid(row=3,column=3) 

        self.ExitButton = tk.Button(self,text='Exit', command=self.Exited)
        self.ExitButton.grid(row=3,column=4)


    #List the CRUD operations functions    
    def Create(self):
        print ('Create Button Pressed')

    #def Read(self):
    #   print ('Read Button Pressed')

    def Update(self):
        print ('Update Button Pressed')

    def Delete(self):
        print ('Delete Button Pressed')

    def Exited(self):
        print ('Exit Button Pressed')
        exit(0)

class odbc:
    dsn_tns=cx_Oracle.makedsn('localhost','8080',service_name='OracleODBC')
    conn=cx_Oracle.connect(user='wm910',password='wm910','@localhost:1521:xe') 

    def Read(self):
        c=conn.cursor()
        c.execute('select * from Employee where empId = 51')
        for row in c:
            print (row[0], '-', row[1])
        conn.close()


c = Application()
c.master.title('Sample Application')
c.mainloop()

并在模型中使用它:

class AfterDateValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if value.blank?

    other_date = record.public_send(options.fetch(:with))

    return if other_date.blank?

    if value < other_date
      record.errors.add(attribute, (options[:message] || "must be after #{options[:with].to_s.humanize}"))
    end
  end
end