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

时间:2022-08-25 20:43:09

I am having issues updating a record, using save. This is my code so far

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

  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

But for some reason it doesn't get updated, this is the output from my log.

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

  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'

As you can see the raise is fetching the job question, but the answer is nil, even thought it is inserting something into it.

正如你所看到的那样,加注正在解决工作问题,但答案是零,甚至认为它正在插入一些东西。

What am i doing wrong?

我究竟做错了什么?

1 个解决方案

#1


1  

You are updating the JobQuestion with id == 2 and then fetching the one with id == 3

您正在使用id == 2更新JobQuestion,然后获取id == 3的那个

Try to use the same query to find the record.

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

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

#1


1  

You are updating the JobQuestion with id == 2 and then fetching the one with id == 3

您正在使用id == 2更新JobQuestion,然后获取id == 3的那个

Try to use the same query to find the record.

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

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