# Date Validation problem

**URL:** <https://www.ruby-forum.com/t/date-validation-problem/116115>\
**Category:** Rails\
**Created:** [October 2, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/date-validation-problem/116115 "2007-10-02T15:22:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [October 2, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/date-validation-problem/116115/1 "2007-10-02T15:22:18Z")

</div>

Hello,

I have this validation code in my model.

def validate  
errors.add(:last\_action\_date, “some message”) if next\_action\_date \<  
last\_action\_date  
end

Two date select fields.

Without the code, as long as dates are valid (no 31st of February), no  
errors, works ok.

With the code in place, when the next\_action\_date is before the  
last\_action\_date then I get a NoMethodError

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.inject

The validation seems to work, but instead of stopping the code and  
going back into edit mode like all other rails validations, instead it  
gives an error.

Can anyone point me in the right direction?

Thank you,

GPB

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [October 2, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/date-validation-problem/116115/2 "2007-10-02T15:42:54Z")

</div>

To complement my post…

This is the log when the dates pass the validation:

=======

Processing RecordsController#update (for 127.0.0.1 at 2007-10-02  
15:37:27) [POST]  
Session ID: 1199d4ef3d66c49f40253c314a4c5a9c  
Parameters: {“commit”=\>“Edit”, “action”=\>“update”, “id”=\>“10”,  
“controller”=\>“records”, “comment”=\>[“”],  
“record”=\>{“next\_action\_date(3i)”=\>“31”,  
“last\_action\_date(1i)”=\>“2012”, “last\_action\_date(2i)”=\>“6”,  
“last\_action\_date(3i)”=\>“7”, “service\_id”=\>“2”, “mode\_id”=\>“1”,  
“nextaction\_id”=\>“1”, “nextaction2\_id”=\>“1”,  
“next\_action\_date(1i)”=\>“2013”, “station\_id”=\>“1”,  
“next\_action\_date(2i)”=\>“3”}}  
[4;36;1mRecord Columns (0.000000) [0m [0;1mSHOW FIELDS FROM  
records [0m  
[4;35;1mRecord Load (0.000000) [0m [0mSELECT \* FROM records  
WHERE (records.`id` = 10) [0m  
[4;36;1mSQL (0.000000) [0m [0;1mBEGIN [0m  
[4;35;1mRecord Update (0.000000) [0m [0mUPDATE records SET  
`created_at` = ‘2007-10-01 12:27:55’, `station_id` = 1,  
`last_action_date` = ‘2012-06-07’, `nextaction_id` = 1,  
`nextaction2_id` = 1, `service_id` = 2, `mode_id` = 1, `updated_at` =  
‘2007-10-02 15:37:27’, `next_action_date` = ‘2013-03-31’ WHERE `id` =  
10 [0m  
[4;36;1mSQL (0.016000) [0m [0;1mCOMMIT [0m  
Redirected to [http://localhost:3000/records/show/10](http://localhost:3000/records/show/10)  
[4;35;1mStation Load (0.000000) [0m [0mSELECT \* FROM stations  
[0m  
[4;36;1mMode Load (0.000000) [0m [0;1mSELECT \* FROM modes [0m  
[4;35;1mService Load (0.000000) [0m [0mSELECT \* FROM services  
[0m  
[4;36;1mNextaction Load (0.000000) [0m [0;1mSELECT \* FROM  
nextactions [0m  
Completed in 0.07800 (12 reqs/sec) | DB: 0.01600 (20%) | 302 Found  
[[http://localhost/records/update/10](http://localhost/records/update/10)]

=======

This is the log when it fails the validation:

Processing RecordsController#update (for 127.0.0.1 at 2007-10-02  
15:37:37) [POST]  
Session ID: 1199d4ef3d66c49f40253c314a4c5a9c  
Parameters: {“commit”=\>“Edit”, “action”=\>“update”, “id”=\>“10”,  
“controller”=\>“records”, “comment”=\>[“”],  
“record”=\>{“next\_action\_date(3i)”=\>“31”,  
“last\_action\_date(1i)”=\>“2012”, “last\_action\_date(2i)”=\>“6”,  
“last\_action\_date(3i)”=\>“7”, “service\_id”=\>“2”, “mode\_id”=\>“1”,  
“nextaction\_id”=\>“1”, “nextaction2\_id”=\>“1”,  
“next\_action\_date(1i)”=\>“2007”, “station\_id”=\>“1”,  
“next\_action\_date(2i)”=\>“3”}}  
[4;35;1mRecord Columns (0.000000) [0m [0mSHOW FIELDS FROM  
records [0m  
[4;36;1mRecord Load (0.000000) [0m [0;1mSELECT \* FROM records  
WHERE (records.`id` = 10) [0m  
[4;35;1mSQL (0.000000) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.000000) [0m [0;1mCOMMIT [0m  
Rendering actioneditlayoutfalse within layouts/records  
Rendering records/edit

ActionView::TemplateError (You have a nil object when you didn’t  
expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.inject) on line #4 of app/  
views/records/\_form.rhtml:

GPB

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [October 2, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/date-validation-problem/116115/3 "2007-10-02T16:01:04Z")

</div>

You’ve left out the stack trace for the actual exception thrown.

Fred

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [October 2, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/date-validation-problem/116115/4 "2007-10-02T16:36:54Z")

</div>

Hi Fred,

I figured it out. Had to do with the placement of the find statements  
for creating the arrays in the controller.

They were at the bottom of the “update”, when I put them at the top it  
solved the problem.

Thank you anyway for answering.

GPB

On Oct 2, 3:59 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
