Multiple Model Creat idiom question

I have the case where creating a role for a non-existent entity requires, naturally enough, that the entity be created as well.

class Entity   has_one :client

class Client   belongs_to :entity

I have this code in my entity_clients_controller

  def create     @entity = Entity.new(params[:entity])

    # need .datebalk! to strip out observer attributes for datebalks plugin     # see config/initializers/hash_addins.rb

    @client = @entity.build_client(params[:client].datebalk!)

    respond_to do |format|       if @entity.save && @client.save

This works, to a point. My problem is that when a validation error is thrown in the client role the entity is created nonetheless. Thus, when the validation error is corrected then the error then becomes that the entity is already on file. So, what is the correct idiom for handling this in create action? Do I do a trial save on the dependent model first?

Hi --

Or

Hi --

David A. Black wrote:

True, but it's nice to get the model to do the work. Also you'd have a bit of race condition (for instance, if someone else saved a record with a unique field that was the same as one of yours) -- though I'm not at all sure that validates_associated would solve that part of it either.

In this particular case, the uniqueness attribute is enforced at the DBMS level via a unique index on entity_id in clients and on entity_name in entities. The validates_associated in both models notwithstanding. Entity_name is normalized to a lowercase sting with all leading, trailing, and excess whitespace removed.

How about:

... Client.transaction do   @client.save   @entity.save end

respond_to do |format|   if @client.new_record?     ... failure response ...   else     ... success response ...   end end

The transaction will roll back @client if it creates successfully but @entity fails.