在 rails 的 ruby 中一次性为所有用户创建出勤

Create attendance for all users at once in ruby on rails

我必须要以下型号:

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

在我的考勤控制器中我有:

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

我的嵌套路线:

Rails.application.routes.draw do

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

我想要实现的是为注册该课程的用户创建课程出勤率。任何使这成为可能的建议或帮助将不胜感激。我见过类似的问题,但它们的结构与我不同。 截至目前,当我点击新出勤时,我只得到一个记录创建表格,但我希望它显示每个学生的所有学生姓名和出勤字段,所以我可以一口气提交。我试了两天都没有运气,所以请帮忙。 提前致谢。

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

您列出了相当复杂的关联,我不确定我是否遵循了您尝试执行的确切程序。无论如何,一旦您理解了创建关联记录的基本概念,您应该能够根据您的具体情况对其进行调整。

所以,结束你所说的:

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

假设您有以下关联:

  • 课程有很多用户
  • 上课人数很多
  • 出勤率有 user_id 列

并假设您有一个 lesson_id 开始。

那么你可以这样写:

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

或类似的东西,以便为属于课程的每个用户创建出勤率。