Rails只调用我的after_create函数的第一行

时间:2014-10-06 03:25:08

标签: ruby-on-rails

我正在尝试在创建用户时动态生成示例调查。在创建Survey但未创建Questions/Answers时,似乎调用了after_create函数。知道他们为什么没有被创造出来吗?

class User < ActiveRecord::Base
  after_create :example_survey
  has_many :surveys

  def example_survey
    survey = Survey.create(title: "Example Survey", survey_limit: 500, number_taken: 376, status: "In Progress", user_id: id, completion_date: Date.today + 14)
    genderQuestion = Question.create(title: "What's your sex?", single_response: true, survey_id: survey.id)
    answer1 = Answer.create(title: "Male", question_id: genderQuestion.id)
    answer2 = Answer.create(title: "Female", question_id: genderQuestion.id)
    ageQuestion = Question.create(title: "What's your age?", single_response: true, survey_id: survey.id)
    answer1 = Answer.create(title: "14-17", question_id: ageQuestion.id)
    answer2 = Answer.create(title: "18-24", question_id: ageQuestion.id)
    answer3 = Answer.create(title: "25-34", question_id: ageQuestion.id)
    answer4 = Answer.create(title: "35-44", question_id: ageQuestion.id)
    answer5 = Answer.create(title: "45-54", question_id: ageQuestion.id)
    answer6 = Answer.create(title: "55+", question_id: ageQuestion.id)
    incomeQuestion = Question.create(title: "What's your household income?", single_response: true, survey_id: survey.id)
    answer1 = Answer.create(title: "<30,000", question_id: incomeQuestion.id)
    answer2 = Answer.create(title: "30,000-50,000", question_id: incomeQuestion.id)
    answer3 = Answer.create(title: "50,001-80,000", question_id: incomeQuestion.id)
    answer4 = Answer.create(title: "80,001-110,000", question_id: incomeQuestion.id)
    answer5 = Answer.create(title: "110,001-150,000", question_id: incomeQuestion.id)
    answer6 = Answer.create(title: "150,001+", question_id: incomeQuestion.id)
  end

end

1 个答案:

答案 0 :(得分:0)

所以我做了rake log:clear并将@Amadan的记录器语句添加到我的代码中,它刚刚开始工作。不知道发生了什么变化,但它现在有效:)。