Forum: Ruby on Rails Split Validations?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7238038e32eb30a16098833e3d61cb19?d=identicon&s=25 Bob Boyken (bboyken)
on 2006-03-31 19:13
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
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-03-31 19:30
(Received via mailing list)
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
7238038e32eb30a16098833e3d61cb19?d=identicon&s=25 Bob Boyken (bboyken)
on 2006-03-31 19:42
Ben Reubenstein 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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-03-31 20:13
(Received via mailing list)
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 Squires
http://somethinglearned.com
81279142caec7e29150fced7f916da62?d=identicon&s=25 DeLynn Berry (Guest)
on 2006-03-31 20:28
(Received via mailing list)
> 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 Berry
delynn@gmail.com
http://www.delynnberry.com
7238038e32eb30a16098833e3d61cb19?d=identicon&s=25 Bob Boyken (bboyken)
on 2006-03-31 20:32
Trevor Squires 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!
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-03-31 21:38
(Received via mailing list)
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
This topic is locked and can not be replied to.