Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling of to_many relation with accepts_nested_attributes_for indexes problem #18870

Closed
knagode opened this issue Feb 10, 2015 · 1 comment

Comments

@knagode
Copy link
Contributor

knagode commented Feb 10, 2015

I have vehicle has many vehicle_services relation. I am trying to insert vehicle_services via accepts_nested_attributes_for method.

Vehicle.rb

class Vehicle < ActiveRecord::Base
  has_many :vehicle_services
  accepts_nested_attributes_for :vehicle_services, :reject_if => lambda { |service| service[:service_on].blank? }

If I try to save vehicle with not valid vehicle_services params I do not get information about which child relation did cause error(s).

Method:

vehicle_params = {"licence_plate"=>"PO-XYZ", 
                  "vehicle_services_attributes"=>
                       {"0"=>{"service_on"=>"2015-11-11"}, 
                        "1"=>{"service_on"=>"SHOULD_RAISE_ERROR-not-correct-date"} ...}
vehicle = Vehicle.create(vehicle_p)
puts vehicle.errors.to_json

Outputs:

{"vehicle_services.service_on":["wrong date format"]}

In vehicle variable I didn't get any information about which vehicle_service is causing errors. Was it index 0 or 1? I would expect errors like:

{"vehicle_services.1.service_on":["wrong date format"]}
@rafaelfranca
Copy link
Member

This is a dup of #8638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants