Validation order firing

If you have:

validates_presence_of
validates_uniqueness_of

validates_each

What fires in what order?

Ok, well the value in the validates_each is giving me nil, not sure why.
Error is:

The error occurred while evaluating nil.>=):

validates_presence_of :code
validates_uniqueness_of :code
validates_presence_of :valid_from_date, :expiry_date
validates_each :expiry_date do |record, attribute, value|
record.errors.add(:expiry_date, ‘must be after or the same as valid
from
date’) unless value >= record.valid_from_date
end

On Wed, Mar 9, 2011 at 10:34 AM, Me [email protected] wrote:

date’) unless value >= record.valid_from_date
end

the error says that value is nil so check first if it’s nil or not.

validates_each :expiry_date do |record, attribute, value|
record.errors.add(:expiry_date, ‘must be after or the same as valid
from
date’) if value && !(value >= record.valid_from_date)
end

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Wed, Mar 9, 2011 at 10:19 AM, Me [email protected] wrote:

the way you order them in your model file. so validates_presence_of goes
first,
uniqueness next then validates_each.


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Wed, Mar 9, 2011 at 11:36 AM, Me [email protected] wrote:

But is the validates_presence_of supposed to catch that first?

ah ok, now i see what you mean.

validations don’t work that way. all validations are run even when the
previous
validations fail. that’s why you need to add :if and :except arguments
so
that
some validations are run only when some conditions are met.

On Wed, Mar 9, 2011 at 10:19 AM, Me [email protected] wrote:

What fires in what order?
To post to this group, send email to [email protected]


To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

But is the validates_presence_of supposed to catch that first?

On 9 March 2011 02:28, Jim Ruther N. [email protected] wrote:

What fires in what order?

the way you order them in your model file. so validates_presence_of goes
first,
uniqueness next then validates_each.

Really? I’m not 100% sure that’s right - although happy to be corrected.

I’ve not looked at the source, but I was under the impression the
validations were added to a hash that was iterated when “valid?” is
called, and a hash returns its elements in no guaranteed order…

I tend to do stuff like:

validates_presence_of :foo
validates_uniqueness_of :foo, :if => :foo
validates_length_of :foo, :maximum => 255, :if => :foo

On 9 March 2011 03:40, Jim Ruther N. [email protected] wrote:

some validations are run only when some conditions are met.
It’s a very annoy feature of websites that tell you one error at a time:

“secret phrase cannot be blank”
so you enter a phrase…
“secret phrase must be longer than 6 characters”
so you frown and make it longer
“secret phrase can only contain alpha-numeric characters”
so you growl and remove the punctuation and tweak the phrase
“secret phrase must be no longer than 20 characters”
you shop elsewhere…

OK, cool. Thanks.

On Wed, Mar 9, 2011 at 4:38 PM, Michael P. [email protected]
wrote:

validations were added to a hash that was iterated when “valid?” is
called, and a hash returns its elements in no guaranteed order…

i’m not sure with rails 3, but i’m pretty sure with rails 2.x as i
remember
adding comments in the models reminding coders to refrain from changing
the order of validations. since changing the validation orders changes
the
order of the errors shown in the view.

but now that you mention it, maybe the order of when validations are run
is different with the order of errors shown.

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Mar 9, 9:04am, Jim Ruther N. [email protected] wrote:

i’m not sure with rails 3, but i’m pretty sure with rails 2.x as i remember
adding comments in the models reminding coders to refrain from changing
the order of validations. since changing the validation orders changes the
order of the errors shown in the view.

but now that you mention it, maybe the order of when validations are run
is different with the order of errors shown.

The errors are stored in a hash (or at least used to be) so ordering
isn’t guaranteed on ruby 1.8

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs