2013-05-07 102 views
0

我的應用程序旨在計算病假/假期。評論部分是我用來追蹤假期的時間,但不排除週末。我讀了這個答案(Counting days excluding weekends),並且真的試圖將它應用到我的模型中,但是現在我的持續時間方程無論我輸入的數據如何都會回到「0」。從日期範圍中排除週末,

class Furlough < ActiveRecord::Base 
    attr_accessible :duration, :note, :date_from, :date_to, :description, :employee_id 
    belongs_to :employee 
    validates_presence_of :date_from, :date_to, :employee_id, :description 

    # def duration 
    #  (date_to - date_from).to_i 
    # end 

    def duration 
    only_weekdays(date_to..(date_from)) 
    end 

    protected 
    def only_weekdays(range) 
    range.select { |d| (1..5).include?(d.wday) }.size 
    end 


end 

謝謝你的時間!

回答

1

更改duration到:

only_weekdays(date_from..date_to) 
+0

祝福你。其他答案是否有這樣的語法「weekdays_in_date_range(self.due_date ..(Date.today))」,因爲Date.today不是像我的模型中那樣的固定變量嗎? – Alaric 2013-05-07 16:27:26

+2

不需要。您可以隨意添加任意數量的不需要的括號。 :-) – 2013-05-07 16:29:20

+0

哈哈,很高興知道:D – Alaric 2013-05-07 16:30:48