问题描述
我想把一个可以跟踪用户出席的应用程序(ruby on rails + mysql)放在一起,但是我正在努力地将数据组织到表中。我会感谢有关可能的表/迁移结构的建议。以下是我到目前为止已经提出的:出席人士表
date DATE
user_id INT
status INT
基本上,我想(非常像出席登记处)标记一个全天/半天或缺席的用户。而且我想能够查看一个用户的考勤记录,变动期(即30天,15天,3天等)。
我将使用三种模式:日,用户和记录。
class Day< ActiveRecord :: Base
has_many:records
end
class User< ActiveRecord :: Base
has_many:records
end
class Record< ActiveRecord :: Base
belongs_to:user
belongs_to:day
end
Day
显然有一个日期字段,而 Record
将有一个状态字段来保存用户是否存在,当天缺席等等。
这样构造就可以让用户查询,日期(包括范围),状态或三者的任意组合。
例如,计算用户的缺勤值:
Record.where (:user_id => @ user.id,:status =>'absent')。count
I want to put together an application (ruby on rails + mysql) that can track "user" attendance, however, I'm struggling with how to organize the data into tables. I'd appreciate suggestions on possible table/migration structures. Following is what I've come up with thus far:
attendance table
date DATE
user_id INT
status INT
Essentially, I want to (much like an attendance register) mark a "user" present for a full-day/half-day or absent. And I'd like to be able to review the attendance record for a user, for a variable period (i.e. past 30 days, 15 days, 3 days, etc.)
Thanks.
I would use three models: Day, User, and Record.
class Day < ActiveRecord::Base
has_many :records
end
class User < ActiveRecord::Base
has_many :records
end
class Record < ActiveRecord::Base
belongs_to :user
belongs_to :day
end
Day
would obviously have a date field, and Record
would have a status field to hold whether the user was present, absent, etc, on that day.
Structuring it like this allows you to query by user, date (including ranges), status, or any combination of the three.
For example, counting a user's absences:
Record.where(:user_id => @user.id, :status => 'absent').count
这篇关于移民与出勤跟踪应用模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!