mercredi 21 septembre 2016

Issue with rails saying a nested model's field is blank when it is not

I'm having an issue with getting a nested model to save properly when it has validations. This seems to only be happening to a single field in this model.

I have two models, a Veterinarian

class Veterinarian < ApplicationRecord
  has_many :licenses
  accepts_nested_attributes_for :licenses, allow_destroy: true, reject_if: :all_blank
end

and a License

class License < ApplicationRecord
  belongs_to :veterinarian
  validates :number, :expiration_date, presence: true
end

My controller action is doing nothing but calling Veterinarian.create(vet_params) with vet_params looking like this rendered out as JSON.

{  
  "zip_code":"",
  "title":"",
  "bio":"",
  "photo":"",
  "licenses_attributes":{  
    "0":{  
      "number":"6436436446",
      "expiration_date":"09/06/2018",
      "_destroy":"false"
    },
    "1":{  
      "number":"Test Number",
      "expiration_date":"09/16/2020",
      "_destroy":"false"
    },
    "2":{  
      "number":"test 2",
      "expiration_date":"09/30/2016",
      "_destroy":"false"
    }
  }
}

The proper params are being sent to 'create()' however with the nested licenses it keeps throwing an error saying expiration_date cannot be blank. This is ONLY happening for additional licenses past the first one. The first one validates just fine.

I am at a loss as to what would be causing this. I've never seen this happen before. Any ideas would be greatly appreciated.

This is Rails 5 btw.

Aucun commentaire:

Enregistrer un commentaire