# How do form data get into ActiveRecord object?

**URL:** <https://www.ruby-forum.com/t/how-do-form-data-get-into-activerecord-object/90167>\
**Category:** Rails\
**Created:** [March 26, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/how-do-form-data-get-into-activerecord-object/90167 "2007-03-26T16:06:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/how-do-form-data-get-into-activerecord-object/90167/1 "2007-03-26T16:06:11Z")

</div>

Hi

I’m quite new to rails and I think until now I didn’t get all of  
ActiveRecord 🙂

So, here is what I want to do:

I have a form where a user can input a date. The logic behind the date  
field should be a bit fancy and allow a special input short cuts:

- “” (blank field) =\> Date.today
- “25” =\> 25th of currnet month
- “23.3” =\> March, 23th in current year…

and so on (I guess you got the clue)

I think I could use client side javascript to do the job, but IMHO  
this is not a good way to do this.

Where can I hook this in in Rails?

For me this belongs into the modle class.

In my controller I have the following line:  
@project\_time = ProjectTime.new(params[:project\_time])

The “date” is one of the params. So which method do I need to  
implement/override to get the chance to  
look at the date passed from the form ?

Thanks for your help

Alex

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/how-do-form-data-get-into-activerecord-object/90167/2 "2007-03-26T16:38:18Z")

</div>

Hi Alex,  
Alex wrote:

> I have a form where a user can input a date. The logic  
> behind the date field should be a bit fancy and allow a  
> special input short cuts:
> 
> - “” (blank field) =\> Date.today
> - “25” =\> 25th of currnet month
> - “23.3” =\> March, 23th in current year…

> Where can I hook this in in Rails?
> 
> For me this belongs into the modle class.

> The “date” is one of the params. So which method do I need to  
> implement/override to get the chance to  
> look at the date passed from the form ?

You might want to define a method in your model to implement the logic  
and  
use a before\_save filter to call it.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/how-do-form-data-get-into-activerecord-object/90167/3 "2007-03-26T16:49:20Z")

</div>

Alex,

You could fiddle with the param in the controller. Here is a mock-up:

now = Time.now  
param\_date = params[:project\_time][:date]  
params[:project\_time][:date] = case  
when /^\d$/ then Time.mktime(now.year, now.month, param\_date)  
…  
else param\_date  
end

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 27, 2007, 8:09am UTC](https://www.ruby-forum.com/t/how-do-form-data-get-into-activerecord-object/90167/4 "2007-03-27T08:09:51Z")

</div>

Hi

Thanks for your help.

I found another solution, which I think is the way to go, as the date  
is corrected just as it is added to the model.  
So the model always holds correct data.

I overwrite the date= method in my model

class ProjectTime \< ActiveRecord::Base

…

def date=(value)

# check value and do the filtering

value = …

# set attribute im my model class

self[:date] = value  
end

end

Alex
