# Overriding a datetime\_select's value with .update\_attributes

**URL:** <https://www.ruby-forum.com/t/overriding-a-datetime-selects-value-with-update-attributes/68508>\
**Category:** Rails\
**Created:** [September 13, 2006, 8:00am UTC](https://www.ruby-forum.com/t/overriding-a-datetime-selects-value-with-update-attributes/68508 "2006-09-13T08:00:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@Dylan\_B](https://www.ruby-forum.com/u/Dylan_B)\
**Post date:** [September 13, 2006, 8:00am UTC](https://www.ruby-forum.com/t/overriding-a-datetime-selects-value-with-update-attributes/68508/1 "2006-09-13T08:00:28Z")

</div>

I just can’t seem to figure this out. I think I’m just looking in the  
wrong place.

On an “edit post” page, I want users to be able to either specify their  
own “created\_at” date/time or let it default to “now”. The way I have  
it set up right now is I have two radio buttons, one with a label of  
“Now” and the other with a label of “Custom”, with a datetime\_select  
set to the post’s created\_at attribute next to the Custom choice.

When they save their changes, it updates the post object with the  
.update\_attributes method. This automatically aggregates the  
datetime\_select’s values and inserts that value in the created\_at  
field.

However, I want to intercept that and have it inject Time.now if the  
user has selected the Now radio button.

I have tried doing a params[:post][:created\_at] = Time.now; before I do  
a @post.update\_attributes(params[:post]); but it doesn’t seem to change  
the created\_at value. It still uses whatever was in the  
datetime\_select.

Any ideas what I’m doing wrong here?

---

<div class="post-metadata">

**Author:** ![Dylan\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@Dylan\_B](https://www.ruby-forum.com/u/Dylan_B)\
**Post date:** [September 13, 2006, 8:22am UTC](https://www.ruby-forum.com/t/overriding-a-datetime-selects-value-with-update-attributes/68508/2 "2006-09-13T08:22:29Z")

</div>

Okay, well, I figured out a way around it, but it seems a pretty sick  
way to do it. I’m really hoping someone else has an answer. Here’s what  
I have to override the datetime\_select’s value…

if params[:postCreatedAt] == “now”  
params[:post][“created\_at(1i)”] = Time.now.year.to\_s  
params[:post][“created\_at(2i)”] = Time.now.month.to\_s  
params[:post][“created\_at(3i)”] = Time.now.day.to\_s  
params[:post][“created\_at(4i)”] = Time.now.hour.to\_s  
params[:post][“created\_at(5i)”] = Time.now.min.to\_s  
end

Please someone agree with me that it’s a pretty pathetic way to do it.  
🙂

---

<div class="post-metadata">

**Author:** ![Dylan\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@Dylan\_B](https://www.ruby-forum.com/u/Dylan_B)\
**Post date:** [September 13, 2006, 8:38am UTC](https://www.ruby-forum.com/t/overriding-a-datetime-selects-value-with-update-attributes/68508/3 "2006-09-13T08:38:30Z")

</div>

On Tue, 2006-09-12 at 23:18 -0700, Dylan B. wrote:

> end
> 
> Please someone agree with me that it’s a pretty pathetic way to do it.

* * *

yeah - I went through that about 3 months ago…I did post up my  
solution but I ended up immediately using the ‘flextimes’ plugin which  
simplified everything and gave me AM/PM (12 hour) designations as well.

Craig
