# Multiple Model Creat idiom question

**URL:** <https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292>\
**Category:** Rails\
**Created:** [April 9, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292 "2008-04-09T16:23:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 9, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/1 "2008-04-09T16:23:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 9, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/2 "2008-04-09T16:28:22Z")

</div>

Hi –

On Wed, 9 Apr 2008, James B. wrote:

> I have this code in my entity\_clients\_controller  
> 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?

You can put validates\_associated :client in the Entity model. Then  
just do @entity.save. They should then either both be saved or neither  
be saved.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 9, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/3 "2008-04-09T16:54:21Z")

</div>

David A. Black wrote:

> You can put validates\_associated :client in the Entity model. Then  
> just do @entity.save. They should then either both be saved or neither  
> be saved.

Works like a charm! Thanks.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 10, 2008, 8:46am UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/4 "2008-04-10T08:46:38Z")

</div>

Or  
On 10/04/2008, at 12:27 AM, David A. Black wrote:

> > 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?
> 
> You can put validates\_associated :client in the Entity model. Then  
> just do @entity.save. They should then either both be saved or neither  
> be saved.
> 
> David

Or, you could validate them both before you save.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 17, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/5 "2008-04-17T12:33:47Z")

</div>

Hi –

On Thu, 10 Apr 2008, Julian L. wrote:

> > > thrown in the client role the entity is created nonetheless. Thus,  
> > > be saved.
> > 
> > David
> 
> Or, you could validate them both before you save.

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.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 17, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/6 "2008-04-17T17:40:23Z")

</div>

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.

On Apr 17, 8:50 am, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 17, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/multiple-model-creat-idiom-question/134292/7 "2008-04-17T14:50:13Z")

</div>

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.
