Migrations & Models in Attendance Tracking Application
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 poss开发者_StackOverflowible 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
精彩评论