0

I'm with Rails 4.

Various table have a relation to a "people" table. This table contain personal information like name, address etc... And a uniqueness identification number (it's not the ID, because it can be null).

validates :identification, :uniqueness => true, :allow_blank => true

The people model is alway registred like a nested attributes. For example, a store can have a responsible person. So when I do the

@store = Store.new(store_params)

or

@store.update(store_params)

I want the associated person was created or just associated and updated depending if the identification number exist or not.

Actualy, if a put a existing Identification, activerecord try to create a new one a send a "already in use" error, because the uniqueness validator.

what would be the right way to do that?

Thanks.

Yoann Augen
  • 1,966
  • 3
  • 21
  • 39
  • You have the solutions to your question here: http://stackoverflow.com/questions/3579924/accepts-nested-attributes-for-with-find-or-create . The simplest is to allow the person id into your submitted params – cristian Jun 09 '14 at 17:48
  • seem to work for the create but not for the update. My autosave_associated_records_for_person it's not call and I have a ActiveRecord::RecordNotFound (Couldn't find Person with ID=12 for Store with ID=13): – Yoann Augen Jun 09 '14 at 22:23

0 Answers0