Forum: Ruby on Rails Newbie stuff (.new method)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B880cd4d3ee4dbc263efb78d64af4cf5?d=identicon&s=25 Bob (Guest)
on 2006-04-14 18:41
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
0a5d6fa912dda67f9b81017fbd9a81f2?d=identicon&s=25 Shane Sherman (Guest)
on 2006-04-14 18:52
(Received via mailing list)
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
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-04-14 18:55
(Received via mailing list)
On 4/14/06, Bob <craigslist.reality.check@gmail.com> 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

-- James
B880cd4d3ee4dbc263efb78d64af4cf5?d=identicon&s=25 Bob (Guest)
on 2006-04-14 18:58
Shane Sherman 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???)?
0a5d6fa912dda67f9b81017fbd9a81f2?d=identicon&s=25 Shane Sherman (Guest)
on 2006-04-14 19:11
(Received via mailing list)
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.
This topic is locked and can not be replied to.