2

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
Idriss
  • 92
  • 9
  • 1
    Please be more precise and describe exactly what you want in your view. – Yorkshireman Jun 23 '16 at 16:42
  • So here is the story: I have users that will be registered to courses and each course has many lessons if you prefer sessions and each session has attendances of the students that are registered to the course. so What I want to do is: take attendance of the students for every session, but how: when I click on the new attendance button, I want to see all users on the view and the options of the attendance [present, late, absent, sick...] and then be able to submit all at once and also be able to edit them later. let me know if this precise anough @Andy – Idriss Jun 23 '16 at 16:48
  • So I'm assuming that you want to go to the Lesson /show view and click a "New Attendance" button; that links to an Attendance /new view, passing the specific instance of Lesson through as a parameter. The view will show a form consisting of all Users that belong to that Lesson's Course. When you submit that form, that will run the Attendance #create method, taking various things from that form as params. Can you show your Attendance model and your Schema? – Yorkshireman Jun 23 '16 at 17:23
  • this is the attendance model:`class Attendance < ActiveRecord::Base belongs_to :user belongs_to :lesson end` it is also on the top – Idriss Jun 23 '16 at 17:25
  • I added on the top below my preview code @Andy – Idriss Jun 23 '16 at 17:36
  • This is one of my favorite questions. Real business problem! In the end, did you manage to build your app? – Yshmarov Dec 07 '20 at 21:39

1 Answers1

2

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.

max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • Okay, I am trying that out. I will let you know how it goes. Thanks a lot for your help. again I will come back to your answer – Idriss Jun 23 '16 at 19:09
  • @Idriss was there an explanation for that comment? – max pleaner Jun 23 '16 at 21:41
  • Thanks to @max pleaner I was able to pass the create action and create the attendance, but it was only for one user at a time. I want to create for all users at once. does anyone knows how to do that? I have: `def new @users.each do |user| @attendance = @lesson.attendances.new(user: user) end end` and my form is: `<%= form_for([@course, @lesson, @attendance]) do |f| %>` – Idriss Jun 23 '16 at 21:47
  • See [http://stackoverflow.com/questions/15317837/bulk-insert-records-into-active-record-table](http://stackoverflow.com/questions/15317837/bulk-insert-records-into-active-record-table) – max pleaner Jun 23 '16 at 22:43
  • So why have you marked this as best answer if you haven't achieved your goal? There isn't much motivation left for me to put the time in to help you now that I won't get any points for doing so. – Yorkshireman Jun 24 '16 at 13:54