未创建父模型时如何将父ID传递给嵌套属性?

时间:2019-06-04 00:43:35

标签: ruby-on-rails ruby model-view-controller activerecord database-design

我有一个学校作业平台,可以: -用户创建具有Work的作业(Work)或测验(Questions) -用户提交作业(Submission

给定的分配或work可以有多个submissionswork也可能有许多questionsquestion可以有多个answers,可以是规范的answers(由老师设置)或非规范的answers(由学生提交)。

当学生提交作业(为给定的Submission创建Work)时,他们应该看到与作业有关的问题,并且能够创建非规范的answers。学生不应更改question的任何属性。

问题:我正在考虑使用嵌套表单来为上述行为建模,但是在填充正确的字段时遇到了一些麻烦。我需要能够将答案与其提交的内容联系起来。但是,此刻,我提交表单并调用Submission.new / Submission.create,但提交的文件还没有ID,因此无法用answers_attribute填充submission_id

问题

  1. 如何修复嵌套表单以正确填充submission_id作为答案?
  2. 还有另一种方法可以完成同一件事吗?
  3. 有没有比现在更简单的关系设计方法?

代码

class Work < ApplicationRecord

  has_many :submissions
  has_many :questions # Allow for quizzes
  has_many :answers, through: :questions
  accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true

  validates :name, presence: true, allow_nil: false


end

# == Schema Information
#
# Table name: works
#
#  id               :bigint(8)        not null, primary key
#  description      :text
#  due_date         :datetime
#  grades_published :boolean          default(FALSE)
#  name             :string
#  points           :float            default(1.0)
#  published        :boolean
#  submittable      :boolean          default(TRUE)
#  url              :string
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  category_id      :integer
#  course_id        :integer
#

class Submission < ApplicationRecord
  belongs_to :work
  belongs_to :enrollment
  has_one :grade
  has_many :questions, through: :works
  has_many :answers, through: :questions

  accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :answers, reject_if: :all_blank, allow_destroy: true

  validates_uniqueness_of :work_id, scope: :enrollment_id, message: " has already been submitted"

end

# == Schema Information
#
# Table name: submissions
#
#  id            :bigint(8)        not null, primary key
#  enrollment_id :integer
#  work_id       :integer
#  title         :string
#  content       :text
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#

class Question < ApplicationRecord
  belongs_to :work
  has_many :answers, inverse_of: :question
  accepts_nested_attributes_for :answers, reject_if: :all_blank, allow_destroy: true

  validates :value, presence: true, allow_nil: false
end

# == Schema Information
#
# Table name: questions
#
#  id         :bigint(8)        not null, primary key
#  value      :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  work_id    :bigint(8)
#
# Indexes
#
#  index_questions_on_work_id  (work_id)
#
class Answer < ApplicationRecord
  belongs_to :question, inverse_of: :answers
  # belongs_to :work, through: :questions
  # belongs_to :submission, through: :works

  validates :question_id, :presence => true
  validates :value, presence: true, allow_nil: false
end

# == Schema Information
#
# Table name: answers
#
#  id            :bigint(8)        not null, primary key
#  is_canon      :boolean          default(FALSE)
#  is_correct    :boolean          default(FALSE)
#  value         :string
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#  question_id   :bigint(8)
#  submission_id :bigint(8)
#
# Indexes
#
#  index_answers_on_question_id    (question_id)
#  index_answers_on_submission_id  (submission_id)
#
# Foreign Keys
#
#  fk_rails_...  (question_id => questions.id)
#  fk_rails_...  (submission_id => submissions.id)
#

非常感谢您的帮助。谢谢!!

0 个答案:

没有答案