2012-03-20 10 views
0

Rails 3ではネストされたアソシエーション()を設定しようとしましたが、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

を、それが失敗したとき、それは 'create'アクションで失敗 – Dipil

+0

をデバッグすることができるので、'保存に、コマンドやバックトレースを投稿してください

サーバー出力が

'、明らかにモデルにエラーはありません。オペレーションにログを追加しました。 – Fraser

答えて

関連する問題