# Validating the presence of two associated objects from both models

**URL:** <https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887>\
**Category:** Rails\
**Created:** [December 31, 2009, 12:31am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887 "2009-12-31T00:31:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 31, 2009, 12:31am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887/1 "2009-12-31T00:31:32Z")

</div>

I have a model “Conversation” and a model “Leader”. A Conversation is  
always led by exactly 1 Leader.

I’ve overridden Conversation’s “validate” method to validate the  
presence of an associated Leader model.

If I add a similar validation to the Leader model, however,  
Conversations can no longer be saved on creation because the Leader  
model is invalid.

On creation of both a new Conversation and new Leader how can I validate  
in each model that it is associated with the other? Is there a best  
practice? The closest I’ve come is to override Leader’s  
“validate\_on\_update” method to ensure the presence of a Conversation but  
that still allows new Leader objects to be created without a  
Conversation, filling my tables with junk.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 31, 2009, 12:40am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887/2 "2009-12-31T00:40:54Z")

</div>

How about this:

class Conversation \< ActiveRecord::Base  
validates\_presence\_of :leader  
end

That way you can’t save a conversation without assigning it a leader,  
but the leader doesn’t have to be saved yet.

On Dec 30, 3:30 pm, Evan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a model “Conversation” and a model “Leader”. A Conversation is always led by exactly 1 Leader.
> 
> I’ve overridden Conversation’s “validate” method to validate the presence of an associated Leader model.
> 
> If I add a similar validation to the Leader model, however, Conversations can no longer be saved on creation because the Leader model is invalid.
> 
> On creation of both a new Conversation and new Leader how can I validate in each model that it is associated with the other? Is there a best practice? The closest I’ve come is to override Leader’s “validate\_on\_update” method to ensure the presence of a Conversation but that still allows new Leader objects to be created without a Conversation, filling my tables with junk.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 31, 2009, 12:52am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887/3 "2009-12-31T00:52:37Z")

</div>

> That way you can’t save a conversation without assigning it a leader,  
> but the leader doesn’t have to be saved yet.

Well, that’s where I’m at right now. I’d like to be in a situation where  
Conversation validates that it has a Leader and Leader validates it has  
a Conversation before either is saved.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 31, 2009, 1:04am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887/4 "2009-12-31T01:04:19Z")

</div>

Evan,

Don’t overdo your requirements. Take your requirements lightly.

From what I can see in your example, Leader plays a larger role in your  
domain, so it has priority over the Conversation.

I would model Leader having zero or more (0…\*) Conversations,  
Conversation  
having 1 Leader. A simple one to many relationship between those domain  
objects.

As from what I can tell, your are trying to create both at the same time  
in  
the view, thus sending both objects over to the controller.

While creating a Conversation, you could give the user the opportunity  
to  
create a Leader, if its currently not available, or he wants to get a  
new  
one. You could use AJAX to access the create action in the leader  
controller  
having it available prior to saving the Conversation.

Check out this resource by Ryan B…

> **[\#57 Create Model Through Text Field - RailsCasts](http://railscasts.com/episodes/57-create-model-through-text-field)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Regards,  
Rodrigo D.  
IBM - IGF Project Manager  
“Communication is the key”

On Wed, Dec 30, 2009 at 8:50 PM, Evan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![evanchaney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/58f4c7/32.png) [@evanchaney](https://www.ruby-forum.com/u/evanchaney)\
**Post date:** [December 31, 2009, 6:41am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-two-associated-objects-from-both-models/180887/5 "2009-12-31T06:41:55Z")

</div>

Evan C. wrote:

> > That way you can’t save a conversation without assigning it a leader,  
> > but the leader doesn’t have to be saved yet.
> 
> Well, thatï¿½s where Iï¿½m at right now. Iï¿½d like to be in a situation where  
> Conversation validates that it has a Leader and Leader validates it has  
> a Conversation before either is saved.

Just validate that there are keys: Conversation validates\_presence\_of  
:leader, Leader validates\_presence\_of :conversation (perhaps). Do the  
rest with foreign key constraints in the DB. As you’ve discovered, the  
application layer is the wrong place for this sort of simple integrity  
check.

> –
> 
> You received this message because you are subscribed to the Google  
> Groups “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid.  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
