Idriss Idriss - 5 months ago 12
Ruby Question

Create attendance for all users at once in ruby on rails

I have to following models:

class User < ActiveRecord::Base
has_many :addresses
has_many :enrollments
has_many :courses, :through => :enrollments
has_many :attendances
has_many :lessons, :through => :courses


class Enrollment < ActiveRecord::Base
belongs_to :user
belongs_to :course


class Course < ActiveRecord::Base
has_many :enrollments
has_many :users, :through => :enrollments
has_many :lessons
has_many :attendances, :through => :lessons

class Lesson < ActiveRecord::Base
belongs_to :course
has_many :attendances
has_many :users, through: :course

class Attendance < ActiveRecord::Base
belongs_to :user
belongs_to :lesson


And in my attendance controller I have:

class AttendancesController < ApplicationController
before_filter :set_lesson
def new
@attendance = @lesson.attendances.new
end
def create
#@attendance = Attendance.new(attendance_params)
@attendance = @course.attendances.new(params[:milestone])
respond_to do |format|
if @attendance.save
format.html { redirect_to @attendance, notice: 'Attendance was successfully created.' }
format.json { render :show, status: :created, location: @attendance }
else
format.html { render :new }
format.json { render json: @attendance.errors, status: :unprocessable_entity }
end
end
end

def set_lesson
@course = Course.find(params[:course_id])
@lesson = @course.lessons.find(params[:lesson_id])
end


my nested routes:

Rails.application.routes.draw do

#resources :attendances
resources :courses do
resources :lessons do
resources :attendances
end
resources :enrollments
end


what I want to achieve is to create an attendance for a lesson for users that are registered to the course of that session. any suggestion or help on making this possible will be appreciated. I have seen similar questions, but they do not have the same structure as me.
as of now when I click on new attendance, I get only one record creation form, but I want it to display all the student name and the attendance field for each student, so I can submit all at ounce. it has been two days i am trying with no luck, so please help.
Thanks in advance.

create_table "attendances", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "lesson_id", null: false
t.string "status", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "attendances", ["lesson_id"], name: "index_attendances_on_lesson_id", using: :btree
add_index "attendances", ["user_id"], name: "index_attendances_on_user_id", using: :btree

create_table "courses", force: :cascade do |t|
t.string "name"
t.string "section"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.date "start_date"
t.date "end_date"
end

create_table "enrollments", force: :cascade do |t|
t.integer "user_id"
t.integer "course_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "enrollments", ["course_id"], name: "index_enrollments_on_course_id", using: :btree
add_index "enrollments", ["user_id"], name: "index_enrollments_on_user_id", using: :btree

create_table "homes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "lessons", force: :cascade do |t|
t.datetime "meeting", null: false
t.datetime "end_time", null: false
t.string "subject", limit: 40, null: false
t.integer "course_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "lessons", ["course_id"], name: "index_lessons_on_course_id", using: :btree

create_table "registrations", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "course_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

Answer

You're laying out a fairly complex of associations and I'm not sure I'm following what exact procedure you're trying to do. Regardless, once you understand the basic concept of creating associated records you should be able to adapt it to your specific scenario.

So, to go off what you said:

what I want to achieve is to create an attendance for a lesson for users that are registered to the course of that session.

Let's say you have the following associations:

  • Lesson has many users
  • Lesson has many attendances
  • Attendance has a user_id column

And say you have a lesson_id you're starting out with.

Then you could write:

lesson = Lesson.find(lesson_id)
users = lesson.users
users.each { |user| lesson.attendances.create(user: user) }

or something along those lines in order to create an attendance for every user that belongs to a lesson.

Comments