# Split Validations?

**URL:** <https://www.ruby-forum.com/t/split-validations/56504>\
**Category:** Rails\
**Created:** [March 31, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/split-validations/56504 "2006-03-31T19:13:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/split-validations/56504/1 "2006-03-31T19:13:13Z")

</div>

I have a single table that two people enter data into. Person A creates  
the record and I need to specify certain required fields in his form.  
Person B has a separate form and she fills in additional fields and I  
need to specify that some of these are required.

Since the data is all in one table and since the validations are in the  
model, won’t Rails complain when person A tries to save? I was getting  
ready to see what would happen, but I thought I’d ask first. Thanks for  
the help!

Bob

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/split-validations/56504/2 "2006-03-31T19:30:03Z")

</div>

If I understand you correctly, there is one table with two distinct  
types.  
You could split this into two tables or use Single Table Inheritance.  
If  
you use STI, you would put a type field in the table and then have 3  
models:

BaseClass  
PersonA \< BaseClass  
PersonB \< BaseClass

Then the PersonA model and PersonB model would have their own  
validation,  
with the common validations in the BaseClass.

Hope this helps,

Ben

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/split-validations/56504/3 "2006-03-31T19:42:39Z")

</div>

Ben R. wrote:

> BaseClass  
> PersonA \< BaseClass  
> PersonB \< BaseClass
> 
> Then the PersonA model and PersonB model would have their own  
> validation,  
> with the common validations in the BaseClass.
> 
> Hope this helps,
> 
> Ben

He’s an example: When we get a trouble call, the receptionist initiates  
a ticket. There are certain fields that are mandatory (name, phone,  
etc.) and certain fields that are optional (title, middle name, etc.).  
Later, someone else opens the ticket, but with a totally different form  
that has many additional fields from the table. Again, some of these  
fields are required and some are optional.

I tried the two table approach and it introduced some unpleasant  
complexities. I will look into single-table inheritance. Thanks!

Bob

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/split-validations/56504/4 "2006-03-31T20:13:06Z")

</div>

Bob,

take a look at the :on parameter to your validations.

You can say:

validates\_something\_or\_other :my\_field, :on =\> :create

you have :on =\> :save (which is the default) or :on =\> :create or :on  
=\> :update

it might fit with the flow you’ve described.

## HTH Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/split-validations/56504/5 "2006-03-31T20:32:01Z")

</div>

Trevor S. wrote:

> Bob,
> 
> take a look at the :on parameter to your validations.
> 
> You can say:
> 
> validates\_something\_or\_other :my\_field, :on =\> :create
> 
> you have :on =\> :save (which is the default) or :on =\> :create or :on  
> =\> :update
> 
> it might fit with the flow you’ve described.
> 
> HTH  
> Trevor

Perfect! This is a simple solution to my problem. Man, I gotta get  
better at asking questions, because as I think through your answer, and  
my problem, I think during :create is the only time we’ll be dealing  
with a partial form.

Thanks!

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/split-validations/56504/6 "2006-03-31T20:28:27Z")

</div>

> Again, some of these fields are required and some are optional.

You might also be interested in the :if parameter which allows you to  
specify a method, proc or string to call to determine if the  
validation should occur.

So you could do something like:

validates\_presence\_of :name, :if =\> current\_user.role == “Receptionist”  
validates\_presence\_of :other\_attribute, :if =\> current\_user.role ==  
“Technician”

Hope that helps.

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com/)**

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 31, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/split-validations/56504/7 "2006-03-31T21:38:46Z")

</div>

Hi again, comments inline:

On 31-Mar-06, at 10:32 AM, Bob Boyken wrote:

> > =\> :update  
> > with a partial form.

My example may have been a little misleading tho. In your scenario it’s  
likely that your going to have the create-only validations have  
no :on parameter  
and the update-only validations will specify :on =\> :update.

Kinda backwards from my original example.

Trev
