# Works on intial show, but not after validation error

**URL:** <https://www.ruby-forum.com/t/works-on-intial-show-but-not-after-validation-error/104822>\
**Category:** Rails\
**Created:** [May 22, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/works-on-intial-show-but-not-after-validation-error/104822 "2007-05-22T22:59:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 22, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/works-on-intial-show-but-not-after-validation-error/104822/1 "2007-05-22T22:59:26Z")

</div>

I have a shipment model with a method called returnable?

If a shipment has been delivered, it figures out what the last  
returnable date is, delivery date + 14 days.

In Shipment Model

def returnable?  
if last\_return\_date.nil?  
return “Yes”  
else  
n = Time.now.to\_i.abs.round  
d = last\_return\_date.to\_i.abs.round  
if d \>= n  
return “Yes”  
else  
return nil  
end  
end  
end

This works great on the intials edit view, but if the form is submitted  
and there is a validation error, it gives the following error message:

NoMethodError in Shipments#update  
undefined method `to\_i’ for #\<Date: 4910233/2,0,2299161\>

Just wondering what’s up?

Both the update action and edit action grab the @shipment instance var  
the same way, so this should totally work.

If I’m just a noob over complicating the “is this date greater than this  
date” thing and there’s a better way to write that, please feel free to  
correct me.

Any help is appreciated.

Thanks,

Dave

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 23, 2007, 12:57am UTC](https://www.ruby-forum.com/t/works-on-intial-show-but-not-after-validation-error/104822/2 "2007-05-23T00:57:34Z")

</div>

On the writing this less complicated front, I think I’ve got a better  
version:

def returnable?  
is\_one = last\_return\_date \<=\> Time.now  
if is\_one \>= 1  
return “Yes”  
else  
return nil  
end  
end

Seems when my form submits and there’s a validation error, my fields of  
type Time get transformed to type Date, so the to\_i wasn’t available as  
a method.

no clue why that is…

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 24, 2007, 12:48am UTC](https://www.ruby-forum.com/t/works-on-intial-show-but-not-after-validation-error/104822/3 "2007-05-24T00:48:33Z")

</div>

I’m thinking you’ve not got a valid shipment object when you recover  
from  
the validation error.  
Can you post your controller code?

Regards,  
Dave

* * *

Information and Educational Technology  
Kwantlen University College - 604-599-2120  
“So powerful is the light of unity that it can illuminate the whole  
earth.” --BahÃ¡'u’llÃ¡h

Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent by: removed\_email\_address@domain.invalid  
22-05-2007 03:57 PM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid  
cc

Subject  
[Rails] Re: works on intial show, but not after validation error

On the writing this less complicated front, I think I’ve got a better  
version:

def returnable?  
is\_one = last\_return\_date \<=\> Time.now  
if is\_one \>= 1  
return “Yes”  
else  
return nil  
end  
end

Seems when my form submits and there’s a validation error, my fields of  
type Time get transformed to type Date, so the to\_i wasn’t available as  
a method.

no clue why that is…

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 24, 2007, 2:45am UTC](https://www.ruby-forum.com/t/works-on-intial-show-but-not-after-validation-error/104822/4 "2007-05-24T02:45:49Z")

</div>

Hi Dave,

I figured this one out. In my database, the date fields were of type  
datetime. However, when I displayed them in the form I was only showing  
mm/dd/yyy via strftime.

So what was a Time object got turned into a Date object if a validation  
error occurred. Since the datetime field only had the date in it anyway,  
I just changed it’s type, changed all my Time.now’s to Date.today’s and  
it’s all fixed.

Thanks for your help,

Dave C
