尝试在Rails 3中设置嵌套关联(使用accepts_nested_attributes_for
),但是save
失败而没有在模型上显示任何错误。
db/schema.rb
:
create_table "question_responses", :force => true do |t|
t.string "answer"
t.boolean "correct"
t.integer "question_id"
t.integer "quiz_result_id"
end
create_table "quiz_results", :force => true do |t|
t.integer "student_id"
t.integer "quiz_id"
t.text "message"
end
question_response.rb
:
class QuestionResponse < ActiveRecord::Base
belongs_to :question
belongs_to :quiz_result, :inverse_of => :question_responses
validates :question, :presence => true
validates :quiz_result, :presence => true
before_save :check_answer
end
quiz_result.rb
:
class QuizResult < ActiveRecord::Base
validates :quiz, :presence => true
validates :student, :presence => true
validates :quiz_id, :uniqueness => { :scope => :student_id }
validate :student_teacher
belongs_to :quiz
belongs_to :student
has_many :question_responses, :dependent => :destroy, :inverse_of => :quiz_result
accepts_nested_attributes_for :question_responses
end
有关如何使其正常工作的任何建议?
服务器输出:
Started POST "/quiz_results" for 127.0.0.1 at 2012-03-20 10:20:24 +0000
Processing by QuizResultsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YceTVCh/GxdReb3KRMNj+cJhm6k0jwhsHl3LcJlSDJM=", "quiz_result"=>{"student_id"=>"695735877", "quiz_id"=>"17663260", "question_responses_attributes"=>{"0"=>{"question_id"=>"14376743", "answer"=>"test"}}}}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 113461968]]
Student Load (0.2ms) SELECT "students".* FROM "students" WHERE "students"."id" = 695735877 LIMIT 1
Teacher Load (0.4ms) SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = 657318460 LIMIT 1
(0.1ms) begin transaction
Quiz Load (0.4ms) SELECT "quizzes".* FROM "quizzes" WHERE "quizzes"."id" = 17663260 LIMIT 1
CACHE (0.0ms) SELECT "students".* FROM "students" WHERE "students"."id" = 695735877 LIMIT 1
QuizResult Exists (0.1ms) SELECT 1 FROM "quiz_results" WHERE ("quiz_results"."quiz_id" = 17663260 AND "quiz_results"."student_id" = 695735877) LIMIT 1
CACHE (0.0ms) SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = 657318460 LIMIT 1
CACHE (0.0ms) SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = 657318460 LIMIT 1
Question Load (0.1ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = 14376743 LIMIT 1
SQL (0.3ms) INSERT INTO "quiz_results" ("created_at", "message", "quiz_id", "student_id", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Tue, 20 Mar 2012 10:20:24 UTC +00:00], ["message", nil], ["quiz_id", 17663260], ["student_id", 695735877], ["updated_at", Tue, 20 Mar 2012 10:20:24 UTC +00:00]]
(0.1ms) rollback transaction
QuestionResponse Load (0.2ms) SELECT "question_responses".* FROM "question_responses" WHERE "question_responses"."quiz_result_id" IS NULL
Rendered quiz_results/_form.html.haml (5.0ms)
Rendered quiz_results/new.html.haml within layouts/application (6.8ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 113461968]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 113461968]]
Student Load (0.1ms) SELECT "students".* FROM "students" WHERE "students"."id" = 695735877 LIMIT 1
Completed 200 OK in 37ms (Views: 13.4ms | ActiveRecord: 2.4ms)
答案 0 :(得分:0)
使用validates_associated:http://guides.rubyonrails.org/active_record_validations_callbacks.html#validates_associated