Skip to main content

How do I create an attendance form for student attendance?

General • Asked by Ernesto Gutierrez

I've been racking my brain and just need to ask for help on the creating this function in my app.

Scope: simple app for non-profit for tracking student attendance
What I've done:

  • created the app
  • shared the app with the end people
  • received positive feedback

What's missing for them is the ability to record attenance for multiple students at once. Without this they can not see the benefit of using this and I'm stuck maitaining an google sheet spreadsheet for them.

This (from what I've read) should be done in two steps:
Step 1 - record the date/time of the attendance for multiple students
Step 2 - record the hours / record you want to track for those that are present
https://stackoverflow.com/questions/30662221/rails-4-attendance-system
https://stackoverflow.com/questions/11991892/rails-3-taking-attendance-form-how-to-create-multiple-records
https://stackoverflow.com/questions/40485042/generate-attendance-view-form-in-rails-for-all-students

  • a dozen more

Form 1 - mark students attendnace
https://docs.google.com/drawings/d/1sM-quaY87Xp86Utp1yQTIrqwTRjzn3k1Fqq92MadaG8/edit
for now I'm just sticking with checkboxes but i'm sure I'll move to something more dynamic with java script or something else but for now let's keep it simple.

From 2 - mark hours / record for present students
https://docs.google.com/drawings/d/10zi_c7dxHMdGC3PpUhQh70N4rFN_0eYMi5wveGxy-gs/edit
I'm getting fancy I know but the idea would be select all / select one / select a few and record hours for each student.

Models

class Student <ActiveRecord::Base
    has_many :attendances, dependent: :destroy
    validates :first_name, :last_name, :school_grade, :team_name, :presence => true
end
class Attendance < ActiveRecord::Base
    belongs_to :student
    has_many :attendance_records
end
class Records <ActiveRecord::Base
    belongs_to :attendance
    belongs_to :student through: :attendance
end

I work on this in the evenings after my kids go to sleep so any help will significantly help reduce my intake to normal healthy levels.


Redirected this question to the Slack Group got this response:

class AttendancesController < ApplicationController
  def mark
    @students = Student.where(id: params[:student_ids])

    ActiveRecord::Base.transaction do
      @students.each do |student|
        attendence = student.attendences.create(date: attendence_params[:date])
      end
    end
  end

  private

  def attendence_params
    params.require(:attendence).permit(:date)
  end
end
class AttendancesController < ApplicationController
  def mark
    @attendance_day = AttendanceDay.find(params[:attendance_day_id])
    @attendance_day.student_ids = params[:student_ids]
    @attendance_day.save
  end
end

I'm a visual learner so having this to refer back to actually helped me understand how I can take an array like student_ids collected from a form and have the controller irreate on them to create in my case an attendance record. Currently there's a has_many / belongs_to association between the students & attendances model.

Suggestions:
With the reponses I may end up uisng a joiner model to bridge Students & Attendnances with a has_many, through this way the end result would be:

class Student < ApplicationRecord
  has_many :attendances
    has_many :records, :through => :attendances
end
class Attendance < ApplicationRecord
  belongs_to :record
    belongs_to :student
end
class Record < ApplicationRecord
  has_many :attendances
    has_many :students, :through => :attendances
end

Can you share the git repo of your app, I can have a look

Using** cocoon** gem and some styling, you can mass mark student attendance (statuses in my case).
schedulable gem - good for scheduling & persisting events in the database
Screenshof from my app:

https://ibb.co/nBAunG


Nested forms make sense to me in theory but in practice I can't seem to see the implementation of it in an attendance tracker.
Here's the repo:
https://github.com/ernestozgutierrez/squashup/tree/models

Here's a quick view of what I'm shooting for: https://goo.gl/V8Jbgy

What I'm trying to do is setup an attendance app similar to https://www.classdojo.com/
I'm not trying to clone it 1for1 but I'm trying to match the abilty to select multiple students from a list / table and mark them as [present, absent, excused, other]. Once they are marked with an attendance only those present are visible to take the next action mark hours of participation.

The part I've been stuck on is the select multiple and assign an attendance type. If you read from the top post I've been looking into this but can't seem to get it right.

The last 2 suggestions had me to the following to the AttendancesController

def mark_student #this is the most recnet attempt

    @students = Array(params[:student_ids].split(",")
    @students.each do |s|
     student = Student.find(s)
     student.attendances.build!
    end

    # @students = params[:student_ids].split(",") #this was the second suggestion
    # @students.each do |s|
    #  student = Student.find(s)
    #  student.attendances.create
    # end

    redirect_to attendances_url

  end
    ```

HI, i am really happy to find your question and what you seek to accomplish as I am trying to build an add-on to my non-profits app for the exact same purpose. Where you able to solve and if so, do you mind sharing?

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.