为什么我的记录没有更新?

时间:2014-01-07 15:10:57

标签: ruby-on-rails ruby activerecord

我在使用保存更新记录时遇到问题。到目前为止,这是我的代码

  def update
    if current_user.customer?
      question = JobQuestion.find(job_question_params[:job_id])
      question.answer = job_question_params[:answer]

      # Only save, if the current user is the owner of the job.
      if current_user.id == Job.find(job_question_params['job_id']).customer.id && question.save
        raise JobQuestion.last.inspect
        render json: { status: 201 }
      end
    end
  end

但由于某种原因它没有得到更新,这是我日志的输出。

  SQL (85.6ms)  UPDATE "job_questions" SET "answer" = $1, "job_id" = $2, "question" = $3, "updated_at" = $4 WHERE "job_questions"."id" = 2  [["answer", "safsf"], ["job_id", 2], ["question", "test test test"], ["updated_at", Wed, 08 Jan 2014 15:04:16 UTC +00:00]]
   (11.5ms)  COMMIT
  JobQuestion Load (0.9ms)  SELECT "job_questions".* FROM "job_questions" ORDER BY "job_questions"."id" DESC LIMIT 1
Completed 500  in 218ms

RuntimeError - #<JobQuestion id: 3, job_id: 2, company_id: 2, question: "test test test", answer: nil, created_at: "2014-01-08 11:47:34", updated_at: "2014-01-08 14:17:00">:
  app/controllers/api/job_questions_controller.rb:27:in `update'
  actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'

正如你所看到的那样,加薪是提出工作问题,但答案是零,甚至认为它正在插入一些内容。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在使用JobQuestion更新id == 2,然后使用id == 3

获取JobQuestion.find(2).inspect #=> #<JobQuestion id: 2,... JobQuestion.last.inspect #=> #<JobQuestion id: 3,...

尝试使用相同的查询来查找记录。

{{1}}
相关问题