2012-03-31 100 views
1

編輯:如果下面的問題看起來有點'廣泛',摘要是我只想在我的模型中結合一個日期字段和一個可選時間字段純粹是爲了目的驗證它使用日期驗證程序,但我無法讓我的測試正確地失敗,當我傳遞一個字符串作爲時間。驗證日期和時間字段一起在軌道模型


編輯2:既然我仍然在努力找到一種方式加入Date對象進行驗證的目的,時間對象,然後我想,如果我可以驗證該時間值是一個Time對象使用is_a?(Time)(而不是一個無效的字符串),但似乎並沒有工作,我想知道是否因爲Time mention with this method(但我不太瞭解它足以知道是否導致問題或不。

我也想知道to_time method是否可以幫助我檢查是否有Time對象,但我不知道如何。

希望有人可以建議如何加入Date對象與Time對象爲驗證的目的,或至少告訴我如何我可以檢查提交的時間值是一個Time對象,而不是一個無效的字符串或東西。


編輯3:我剛剛試過以下基於另一個答案,我發現,但它仍然沒有抱怨在測試「無效」的字符串:

validate :start_time_is_time?, :if => "start_time.present?" 

def start_time_is_time? 
    unless start_time.is_a?(Time) || ((Time.parse(start_time) rescue ArgumentError) == ArgumentError) 
    errors.add(:start_time, 'must be a valid time') 
    end 
end 

有爲什麼字符串可能會被視爲有效的Time


我有類似Trying to set a variable in before_validation but it isn't working與使用日曆來選擇日期和下拉菜單選擇時間圖的圖。我嘗試將其應用於我的模型,但由於我對ruby有點新,並且無法填補答案留下的空白,所以無法實現它。

我的模型有start_date和start_time字段。

我需要組合這兩個字段,以便我可以將它們一起驗證爲datetime(使用date_validator gem),而不必單獨驗證日期和時間,儘管它們將作爲單獨的字段放入數據庫中,除非有人能夠說服我,否則(儘管我已經與他們分開做了很多,所以不想改變這一點)。

必須提交日期,但時間字段是可選的。

從另一個問題看來,我需要一個before_validation方法,可以根據需要將2個字段組合成一個虛擬字段,然後我可以使用date_validator gem進行驗證。

編輯:原來的錯誤已經糾正了感謝RyanJM,但我仍然無法讓測試通過正確。以下是目前的比賽狀態。

這裏是我的欣欣模型的基礎知識,現在:

class Showtime < ActiveRecord::Base 

    attr_accessible :start_date, :start_time 

    attr_accessor :starting_at, :starting_time 

    belongs_to :performance 

    before_validation :construct_starting_at 

    validates :start_date, :presence => true, :date => { :after_or_equal_to => Proc.new { Date.today } } 
    validates :starting_at, :date => { :after_or_equal_to => Proc.new { Time.now } }, :if => "start_date.present? && start_time.present?" 

    def construct_starting_at 
    if start_date.present? && start_time.present? 
     self.starting_time = self.start_time.strftime("%T") 
     self.starting_at = DateTime.strptime("#{start_date} #{starting_time}", "%F %T") 
    end 
    end 

end 

在情況下,它是必需的,這裏的基礎知識/性能模型的相關部分:

class Performance < ActiveRecord::Base 

    has_many :showtimes, :dependent => :delete_all 
    accepts_nested_attributes_for :showtimes, :allow_destroy => true, :reject_if => lambda { |a| a[:start_date].blank? } 

end 

下面是失敗的測試:

require 'spec_helper' 

describe Showtime do 

    before(:each) do 
    @attr = { 
     :name => "Performance 1", 
     :showtimes_attributes => { 
       "0" => { 
        :start_date => Date.today+15.days, 
        :start_time => Time.now 
       } 
     } 
    } 
    end 

    it "should test that the start time is a valid time if it exists" do 
    @attr_invalid = { 
     "0" => { 
      :start_date => Date.today+15.days, 
      :start_time => "invalid" 
     } 
    } 
    invalid = Performance.create(@attr.merge(:showtimes_attributes => @attr_invalid)) 
    invalid.should_not be_valid 
    end 

end 

希望我沒有在上面的代碼中打破任何東西,只是爲了顯示ne子宮頸部位。

+0

它是如何在實際START_TIME在數據庫中定義的? 是否將start_date定義爲數據庫中的日期時間? – RadBrad 2012-03-31 23:06:44

+0

'start_date'被定義爲'date','start_time'被定義爲'time'。我將它們保存爲獨立的字段,以便'start_time'可以是可選的。 – user1116573 2012-04-03 12:41:59

+0

如果start_time是可選的,那麼當你準備設置starting_at時,檢查它是什麼?你可能需要兩個案件嗎?一個是當它在那裏,一個是當它不在?在我的情況下,starting_at是我保存到數據庫的對象。你如何聲明變量? – RyanJM 2012-04-05 21:03:10

回答

0

我還沒有使用date_validator寶石,但做這種事情很好,也似乎更積極維護。

+0

感謝您的回覆Thilo。在移至date_validator gem之前,我嘗試使用該寶石。我需要允許空白,並且似乎有一個validates_timeliness的缺陷,允許將它應用驗證的字段留空。 date_validator gem也似乎更簡單一些。 Date_validator似乎沒有驗證時間,所以我試圖合併我的日期和我的時間來得到我可以驗證的日期時間。 – user1116573 2012-04-14 15:35:51