Nested models when id is unavailable

I have a controller that’s creating model objects from XML input.
Something like:

true Scott

I have two models, StatusReport and Person.

I want to create a new Person iff there is not already one with that
name. Then, link that Person to this StatusReport.

The built-in accept_nested_attributes_for doesn’t work for me since it
will always create a new Person unless I provide an id to an existing

I have full control over the XML input. What I don’t have is a good
way for it to include the id of an existing Person.

Right now I have a fat controller explicitly looking for a Person,
then doing Person.create! if needed. I’m sure there’s a better way.

Just in case anybody cares about the resolution of this, I’ll answer
my own post.

In my StatusReport model, I added:

def person_attributes(params = {})
self.person = Person.find_or_initialize_by_name(params[:name])

Then I changed the XML to:



This works much like accepts_nested_attributes_for.