Date Validation problem

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

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
[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]

=======

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

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

Fred

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. [email protected]

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