MartinElvar
MartinElvar

Reputation: 5804

Why isn't my record getting updated?

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

  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?

Upvotes: 0

Views: 38

Answers (1)

Roberto Decurnex
Roberto Decurnex

Reputation: 2534

You are updating the JobQuestion with id == 2 and then fetching the one with 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,...

Upvotes: 1

Related Questions