When I was creating a model, I made a column edited:timestamp.
t.timestamp :edited # in migration file
Was expecting that whenever user edits anything in this model, the time/date will be updated.
But I just saw that in database schema it is written down as
t.datetime "edited"
and it expects user to enter data in a form.
What’s happening here?
How should I change my migration file to make a timestamp column in SQL DB? Or I should assign it in the model itself?
I don’t want it to depend on user input, has to be a system cell
thx.
When you create a model, you should then generate a ActiveRecord migration to create the table that will store the data associated with this model. For example, if your model is called ‘Product’, run:
this will generate a migration file in db/migrate (prefixed with the timestamp at which you generated the migration file, eg: “20121201200720_create_products_table.rb”)
The migration file should look like this:
When you run this migration (
rake db:migrate), ActiveRecord will automatically create two columns in your table :created_atandupdated_at. Then go and have a look at your schema.rb (in db). There will be something like that:created_atwill store the timestamp at which the product was created.updated_atwill store the timestamp at which the product was last updated (what you’re looking for here)So you don’t have to create a custom column, you should just rely on ActiveRecord’s built-in timestamps.