Newbie stuff (.new method)


#1

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


#2

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


#3

On 4/14/06, Bob 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

– James


#4

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.


#5

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???)?