joshua5201/tioj

ActiveRecord::RecordNotFound while using NestedAttributes

Closed this issue · 2 comments

in branch "rails"

app/models/problem.rb

class Problem < ActiveRecord::Base
  has_one :limit, :inverse_of => :problem
  has_many :submissions
  has_many :testdata
  accepts_nested_attributes_for :limit
end

app/models/limit.rb

class Limit < ActiveRecord::Base
  belongs_to :problem
end

app/controllers/problems_controller.rb

class ProblemsController < ApplicationController
  before_action :set_problem, only: [:show, :edit, :update, :destroy]

  # GET /problems
  # GET /problems.json
  def index
    @problems = Problem.all
  end

  # GET /problems/1
  # GET /problems/1.json
  def show
  end

  # GET /problems/new
  def new
    @problem = Problem.new
    @limit = @problem.create_limit( :time => 1000, :memory => 65536, :output => 65536)
  end

  # GET /problems/1/edit
  def edit
    @limit = @problem.limit
  end

  # POST /problems
  # POST /problems.json
  def create
    @problem = Problem.new(problem_params)
    respond_to do |format|
      if @problem.save
        format.html { redirect_to @problem, notice: 'Problem was successfully created.' }
        format.json { render action: 'show', status: :created, location: @problem }
      else
        format.html { render action: 'new' }
        format.json { render json: @problem.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /problems/1
  # PATCH/PUT /problems/1.json
  def update
    respond_to do |format|
      if @problem.update(problem_params)
        format.html { redirect_to @problem, notice: 'Problem was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @problem.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /problems/1
  # DELETE /problems/1.json
  def destroy
    @problem.destroy
    respond_to do |format|
      format.html { redirect_to problems_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_problem
      @problem = Problem.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def problem_params
      params.require(:problem).permit(:id, :name, :description, :source, :limit, limit_attributes: [:id, :time, :memory, :output, :problem_id])
    end
end

app / views / problems / _form.html.erb

<%= form_for(@problem) do |f| %>
    <% if @problem.errors.any? %>
    <div id="error_explanation">
    <h2><%= pluralize(@problem.errors.count, "error") %> prohibited this problem from being saved:</h2>

    <ul>
        <% @problem.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
    <% end %>
    </ul>
</div>
  <% end %>

<div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
</div>
<div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
</div>
<div class="field">
    <%= f.label :source %><br>
    <%= f.text_area :source %>
</div>
<div class="field">
    <%= f.fields_for :limit do |ff| %>
        <%= ff.label :time, "Time Limit"%>
    <%= ff.text_field :time %>
</div>
<div class="field">
    <%= ff.label :memory, "Memory Limit" %>
    <%= ff.text_field :memory %>
</div>
<div class="field">
    <%= ff.label :output, "Output Limit" %>
    <%= ff.text_field :output %>
</div>
  <% end %>
<div class="actions">
    <%= f.submit %>
</div>
<% end %>

error message

ActiveRecord::RecordNotFound in ProblemsController#update
Couldn't find Limit with ID=18 for Problem with ID=14
app/controllers/problems_controller.rb:45:in `block in update'
app/controllers/problems_controller.rb:44:in `update'

parameters

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"AJ32CXmRqfX++Lu2/kbVY1PYIdriDAV9grAPNE86YMw=",
 "problem"=>{"name"=>"1",
 "description"=>"1",
 "source"=>"1",
 "limit_attributes"=>{"time"=>"1000",
 "memory"=>"65536",
 "output"=>"65536",
 "problem_id"=>"14",
 "id"=>"18"}},
 "commit"=>"Update Problem",
 "id"=>"14"}

other people's Stack overflow issue

Webrick Console Log

Started GET "/problems/new" for 127.0.0.1 at 2014-02-02 03:15:20 +0800
Processing by ProblemsController#new as HTML
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "problems" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", Sat, 01 Feb 2014 19:15:20 UTC +00:00], ["updated_at", Sat, 01 Feb 2014 19:15:20 UTC +00:00]]
  SQL (0.1ms)  INSERT INTO "limits" ("created_at", "memory", "output", "time", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sat, 01 Feb 2014 19:15:20 UTC +00:00], ["memory", 65536], ["output", 65536], ["time", 1000], ["updated_at", Sat, 01 Feb 2014 19:15:20 UTC +00:00]]
   (302.0ms)  commit transaction
  Rendered problems/_form.html.erb (5.0ms)
  Rendered problems/new.html.erb within layouts/application (5.8ms)
Completed 200 OK in 318ms (Views: 11.9ms | ActiveRecord: 302.5ms)


Started PATCH "/problems/14" for 127.0.0.1 at 2014-02-02 03:15:25 +0800
Processing by ProblemsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AJ32CXmRqfX++Lu2/kbVY1PYIdriDAV9grAPNE86YMw=", "problem"=>{"name"=>"1", "description"=>"1", "source"=>"1", "limit_attributes"=>{"time"=>"1000", "memory"=>"65536", "output"=>"65536", "problem_id"=>"14", "id"=>"18"}}, "commit"=>"Update Problem", "id"=>"14"}
  Problem Load (0.1ms)  SELECT "problems".* FROM "problems" WHERE "problems"."id" = ? LIMIT 1  [["id", "14"]]
   (0.1ms)  begin transaction
  Limit Load (0.1ms)  SELECT "limits".* FROM "limits" WHERE "limits"."problem_id" = ? ORDER BY "limits"."id" ASC LIMIT 1  [["problem_id", 14]]
   (0.0ms)  rollback transaction
Completed 404 Not Found in 3ms

ActiveRecord::RecordNotFound (Couldn't find Limit with ID=18 for Problem with ID=14):
  app/controllers/problems_controller.rb:45:in `block in update'
  app/controllers/problems_controller.rb:44:in `update'


  Rendered /home/joshua5201/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
  Rendered /home/joshua5201/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /home/joshua5201/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /home/joshua5201/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.0ms)


Started PATCH "/problems/14" for 127.0.0.1 at 2014-02-02 03:22:44 +0800
Processing by ProblemsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AJ32CXmRqfX++Lu2/kbVY1PYIdriDAV9grAPNE86YMw=", "problem"=>{"name"=>"1", "description"=>"1", "source"=>"1", "limit_attributes"=>{"time"=>"1000", "memory"=>"65536", "output"=>"65536", "problem_id"=>"14", "id"=>"18"}}, "commit"=>"Update Problem", "id"=>"14"}
  Problem Load (0.2ms)  SELECT "problems".* FROM "problems" WHERE "problems"."id" = ? LIMIT 1  [["id", "14"]]
   (0.1ms)  begin transaction
  Limit Load (0.1ms)  SELECT "limits".* FROM "limits" WHERE "limits"."problem_id" = ? ORDER BY "limits"."id" ASC LIMIT 1  [["problem_id", 14]]
   (0.1ms)  rollback transaction
Completed 404 Not Found in 17ms

ActiveRecord::RecordNotFound (Couldn't find Limit with ID=18 for Problem with ID=14):
  app/controllers/problems_controller.rb:45:in `block in update'
  app/controllers/problems_controller.rb:44:in `update'


Seems like it didn't do the transaction for the problem_id attribute.... it is ok in rails console..

the issue suddenly fixed...don't know why