我必须遵循以下模型:
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
答案 0 :(得分:1)
您正在制定一个相当复杂的协会,我不确定我是否遵循了您尝试做的具体程序。无论如何,一旦您了解了创建相关记录的基本概念,您就应该能够根据您的具体情况进行调整。
所以,说完你所说的话:
我想要达到的目的是为在该会话过程中注册的用户创建课程。
我们假设您有以下关联:
并说你有一个lesson_id
你开始了。
然后你可以写:
lesson = Lesson.find(lesson_id)
users = lesson.users
users.each { |user| lesson.attendances.create(user: user) }
或类似的东西,以便为属于课程的每个用户创建出勤。