Search code examples
ruby-on-railsnested-formsnested-attributes

RecordNotFound with accepts_nested_attributes_for and belongs_to


I get

ActiveRecord::RecordNotFound: Couldn't find Client with ID=3 for Order with ID=

when trying to submit an Order form for an existing client. This happens through the form or the console by typing:

Order.new(:client_attributes => { :id => 3 })

payment_form.html.erb:

<%= semantic_form_for @order, :url => checkout_purchase_url(:secure => true) do |f| %>

        <%= f.inputs "Personal Information" do %>

            <%= f.semantic_fields_for :client do |ff| %>
                <%= ff.input :first_name %>
                <%= ff.input :last_name %>              
                <!-- looks like semantic_fields_for auto-inserts a hidden field for client ID -->
            <% end %>

        <% end %>
<% end %>

Order.rb:

class Order < ActiveRecord::Base
  belongs_to :client
  accepts_nested_attributes_for :client, :reject_if => :check_client

  def check_client(client_attr)
    if _client = Client.find(client_attr['id'])
      self.client = _client
      return true
    else
      return false
    end    
  end
end

The reject_if idea came from here but I logged the method and it's not even being called! It doesn't matter what its name is!


Solution

  • If you only want a new Order with an existing client, without modifying the client, you need to assign the id.

    Order.new(client_id: 3)
    

    This is another way to do this without overloading the client_attributes= method and cleanest

    The new Order now has the client with ID 3

    If you also want to update ant client's attributes you must add the client_attributes, for example:

    Order.new(client_id: 3, client_attributes: { id: 3, last_order_at: Time.current })
    

    See https://github.com/rails/rails/issues/7256 from 2012.