在我的應用程序用戶has_many手勢。計算用戶完成手勢後的幾天的好方法是什麼?最好的方法來計算一個非破的連續幾天
現在我正在像下面這樣做,它按照我的意願工作。但顯然它沒有規模。
class User < ActiveRecord::Base
# ...
def calculate_current_streak
return 0 unless yesterday = gestures.done_on_day(Date.today-1)
i = 0
while gesture = self.gestures.done_on_day(Date.today - (i+1).days).first
i += 1
end
i += 1 if gestures.done_on_day(Date.today).first
i
end
end
謝謝!特別指向一個誰可以在某種程度上工作,唯一的軌道工作日太:)
你可以解釋一下數據庫表結構 - 你有一個手勢表,每個手勢的記錄和時間戳嗎? – Elad 2011-03-29 11:27:16