# Newbie stuff (.new method)

**URL:** <https://www.ruby-forum.com/t/newbie-stuff-new-method/57838>\
**Category:** Rails\
**Created:** [April 14, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/newbie-stuff-new-method/57838 "2006-04-14T18:41:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 14, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/newbie-stuff-new-method/57838/1 "2006-04-14T18:41:55Z")

</div>

Hi-

Ruby 1.8.2 Rails 1.1

so I’m trying to build my first rails app and I’m trying to understand  
how the helper methods work. For instance .new & .save . For instance,  
I’ve created a table of Objects, and generated a scaffold. I see that  
in the controller, there are calls to Object.new and Object.save. I’m  
assuming that these are methods inherited from ActiveRecord that  
automagically execute SQL statements to insert and update the matching  
records in the table. very cool shortcut. However - say i need to  
“teak” this - I have a feils called “created” of type datetime that I  
want to auto populate whenver a new record is added. How do I modify  
the “new” method to do this without having to write my own custom one?

Thanks

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 14, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/newbie-stuff-new-method/57838/2 "2006-04-14T18:52:43Z")

</div>

Object.new is from Ruby. it creates a new object, it doesn’t save it in  
the  
database or anything. If you have a column named created\_on that’s a  
timestamp on your model it will automatically be populated with the date  
the  
item was created. same with updated\_on.

If it’s something else other than created\_on, updated\_on, then you just  
set  
it…

house = House.new  
house.address = ‘1234 whatever st’  
house.save

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 14, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/newbie-stuff-new-method/57838/3 "2006-04-14T18:55:45Z")

</div>

On 4/14/06, Bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so I’m trying to build my first rails app and I’m trying to understand  
> how the helper methods work. For instance .new & .save . For instance,  
> I’ve created a table of Objects, and generated a scaffold. I see that  
> in the controller, there are calls to Object.new and Object.save. I’m  
> assuming that these are methods inherited from ActiveRecord that  
> automagically execute SQL statements to insert and update the matching  
> records in the table. very cool shortcut. However - say i need to  
> “teak” this - I have a feils called “created” of type datetime that I  
> want to auto populate whenver a new record is added. How do I modify  
> the “new” method to do this without having to write my own custom one?

Close. Object.new just creates a new object. It doesn’t touch your  
database. .save does what you think it does.

If you name that field “created\_at” or “created\_on” Rails will  
populate it for you.

[http://wiki.rubyonrails.org/rails/pages/MagicFieldNames](http://wiki.rubyonrails.org/rails/pages/MagicFieldNames)

– James

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 14, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/newbie-stuff-new-method/57838/4 "2006-04-14T19:11:17Z")

</div>

that’s it! if the columns are there, they will get auto populated. i  
believe  
they can be named created\_on, updated\_on or created\_at, updated\_at.  
There’s  
also a plugin that will do “created\_by” which is really cool.

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [April 14, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/newbie-stuff-new-method/57838/5 "2006-04-14T18:58:27Z")

</div>

Shane S. wrote:

> Object.new is from Ruby. it creates a new object, it doesn’t save it in  
> the  
> database or anything. If you have a column named created\_on that’s a  
> timestamp on your model it will automatically be populated with the date  
> the  
> item was created. same with updated\_on.
> 
> If it’s something else other than created\_on, updated\_on, then you just  
> set  
> it…
> 
> house = House.new  
> house.address = ‘1234 whatever st’  
> house.save

Ahh ok. I didn’t know about created\_on and updated\_on - looks like they  
automate exatly what I’m trying to do. I don’t have to do anything to  
the model .rb file? Just create a column named created\_on that is of  
type timestamp (or datetime???)?
