Tai Tri Vo
Tai Tri Vo

Reputation: 55

rails nested attributes rails record_not_found when update

I have 2 models like:

class Customer < ActiveRecord::Base
 has_many :passengers
 accepts_nested_attributes_for :passengers, allow_destroy: true
end

class Passenger < ActiveRecord::Base
 belongs_to :customer
end

customer_params contain :

:name,...
:passengers_attributes: [:id, :name, :_destroy]

and when pass passengers_attributes to update customer (id=1) like

{
  "passengers_attributes": [
    {
      "name": "abc",
      "id": 5
    }
  ]
}

With passenger "abc" is new record

When i run customer.update_attributes!(customer_params), it raise error ActiveRecord::RecordNotFound: Couldn't find Passenger with ID=5 for Customer with ID=1

Do you know this error? i need your help. Thanks

Upvotes: 2

Views: 1017

Answers (1)

Shefalee Chaudhary
Shefalee Chaudhary

Reputation: 602

Found your error here

{
  "passengers_attributes": [
    {
      "name": "abc",
      "id": 5
    }
  ]
}

You can't send id in parameters here. Id can't be modified or you can insert it manually.

Here your customer don't have passenger with id "5".that's why you are getting this error.

Upvotes: 1

Related Questions