0

在我的Rails应用程序我有一个用户模式,系车型,组模型和注册model.User模型具有基本的用户信息,Rails 3:如何比较日期时间。

用户模型:

id , name 
has_and_belongs_to_many :departments , :groups 

系车型:

id,name 
has_and_belongs_to_many :users 
has_many :registers 

组型号:

id,name 
has_and_belongs_to_many :users 
has_many :registers 

REGIST ER模型:

date ,department_id , group_id , one , two , three 
belongs_to :department ,:group 

其中注册模型“一”,“二”,“三”是时隙说:9-12,12-3,3-6.Every一天每个用户都有标记考勤所以他们的出席必须在登记表中的时间段上进行标记。如何根据当前时间标记出席时间。

在此先感谢。

回答

1

你可能需要做

current_time = Time.now 
if (9..11).include?(current_time.hour) 
# Do A 
elsif (12..14).include?(current_time.hour) 
# Do B 
elsif (15..18).include?(current_time.hour) 
# Do C 
end 

(或)

current_time = Time.now 
if (9...12).include?(current_time.hour) 
# Do A 
elsif (12...15).include?(current_time.hour) 
# Do B 
elsif (15...18).include?(current_time.hour) 
# Do C 
end 
# I think of the extra "." as 'eating' the last value so you don't have it. 

应对重叠

+0

感谢您的回答...什么是(9..11),(12..14),(15..18)在你上面的文章中nt,上面的代码实际上做了什么。 – Cyber

+0

是时候。结构'n1..n2'是一个范围。它基本上意味着,给出数字9,10和11.它包括终点(9和11)。还有一个范围不包括最后一位数字。上面添加了这个。 –

0

可能考虑创建一个位图。一个32位整数是轻量级的,并给你0-24,每小时一个。写一些可以比较,设置...小时范围的功能。

1

试试这个

current_time = Time.now 
if (9..12).include?(current_time.hour) 
# 9-12 
elsif (12..15).include?(current_time.hour) 
# 12-3 
elsif (15..18).include?(current_time.hour) 
# 3-6 
end