我的类方法有问题。
我正在编写一个测验应用,并创建方法Quiz.build_quiz
以生成一个测验,从另一个表格中随机提问。
这些是我的课程
class Quiz < ActiveRecord::Base
has_many :quiz_questions
has_many :questions, through: :quiz_questions
accepts_nested_attributes_for :questions
def self.create_quiz(quiz_type = 'base')
q = Quiz.create
q.questions << Question.order_by_rand.all
q.save
end
end
class Question < ActiveRecord::Base
belongs_to :section
has_many :answers
has_many :quiz_questions
has_many :quizzes, through: :quiz_questions
def self.error_count
self.sum(:quiz_errors)
end
end
如果我在控制台中尝试创建我的对象一切都很好,但是当我调用该方法时,该关联是空的。 错误在哪里?
:(
编辑:我也是这样尝试的:
def self.create_quiz(quiz_type = 'base')
q = Quiz.new
q.questions << Question.order_by_rand.all
q.save!
return q
end