Split Validations?


#1

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


#2

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


#3

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


#4

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


#5

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!


#6

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


#7

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