Is there a way to avoid automatically updating Rails timestamp fields?

rails update touch: false
rails touch
rails update vs update_attributes
rails update_column
rails 5 timestamps
created_at rails
rails override created_at
rspec set updated_at

If you have DB columns created_at and updated_at Rails will automatically set those values when you create and update a model object. Is there a way to save the model without touching those columns?

I am bringing in some legacy data and I would like to set those values from the corresponding values in the (differently named) legacy data fields. I'm finding when I set them on the model and then save the model, Rails appears to override the incoming values.

Of course I could just name the Rails model columns differently to prevent that, but after the data is imported, I want Rails to do its automatic timestamp thing.

Do this in a migration or in a rake task (or in the new database seeds if you're on edge rails):

ActiveRecord::Base.record_timestamps = false
begin
  run_the_code_that_imports_the_data
ensure
  ActiveRecord::Base.record_timestamps = true  # don't forget to enable it again!
end

You can safely set created_at and updated_at manually, Rails won't complain.

Note: This also works on individual models, e.g. User.record_timestamps = false

Is there a way to avoid automatically updating Rails timestamp fields , If you have created_at and updated_at columns in the db table. rails will automatically set these values when you create and update a model object. there are  ruby on rails - Is there a way to avoid automatically updating Rails timestamp fields? If you have DB columns created_at and updated_at Rails will automatically set those values when you create and update a model object.

use update_column method instead:

http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_column

update_column(name, value)
# Updates a single attribute of an object, without calling save.

Validation is skipped.

Callbacks are skipped.

updated_at/updated_on column is not updated if that column is available.

Raises an ActiveRecordError when called on new objects, or when the name attribute is marked as readonly.

How to avoid automatically updating rails timestamp fields ?, We can save a record without updating timestamps in Rails 5. ActiveRecord object then Rails automatically updates fields updated_at or updated_on . as an option to save , we can update the object without updating timestamps. Team · How we work · Presentations · Jobs · Wall of love · Contact Us. If you have DB columns created_at and updated_at Rails will automatically set those values when you create and update a model object. Is there a way to save the model without touching those columns

Rails 5 provides a convenient way to update a record without updating it's timestamp updated_at: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-save

You, just need to pass touch:false while updating your record.

>> user = User.first
>> user.updated_at
=> Thu, 28 Apr 2016 20:01:57 IST +05:30
>> user.name = "Jose"
>> user.save(touch: false)
=> true

>> user.updated_at
=> Thu, 28 Apr 2016 20:01:57 IST +05:30

Rails 5 Updating a record without updating timestamps, In Rails 4.x, when we save an ActiveRecord object then Rails automatically updates fields updated_at or updated_on . Copy In Rails 5, by passing touch: false as an option to save , we can update the object without updating timestamps. ArrayInquirer provides friendlier way to check contents in an array. An "out of the box" datetime ( timestamp without zone written as UTC) field in Rails will not be parsed correctly in a at least a few other postgres clients I've seen. The ruby pg gem doesn't parse it correctly, the sequel gem doesn't, and nodejs pg module does not. And when I say it "doesn't parse it correctly",

You can set the following inside your migration:

ActiveRecord::Base.record_timestamps = false

Or altenatively use update_all:

update_all(updates, conditions = nil, options = {})

Updates all records with details given if they match a set of conditions supplied, limits and order can also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the database. It does not instantiate the involved models and it does not trigger Active Record callbacks.

Update a record without updating timestamps, I found a problem in the way the ActiveRecord::Timestamp class implements The problem is there are 2 ways of temporarily disabling the field from This issue has been automatically marked as stale because it has not The resources of the Rails team are limited, and so we are asking for your help. Chronostamp adds magic timestamp fields to Models, like Ruby on Rails: created_at; updated_at; Features. will add timestamp magic fields (created_at and updated_at) in an unobtrusive way. will automatically record creation and update timestamps of database objects. avoid inheritance coupling just for adding two attributes.

In Rails 3+, for a single object, set record_timestamps for the object rather than class. I.e.,

>> user = User.first
>> user.updated_at
=> Tue, 12 Apr 2016 22:47:51 GMT +00:00
>> user.name = "Jose"
=> "Jose"
>> user.record_timestamps = false
>> user.save
=> true
>> user.updated_at
=> Tue, 12 Apr 2016 22:47:51 GMT +00:00
>> User.record_timestamps
=> true

This way, you don't touch global state for the model, and you don't have to remember to restore the prior setting in an ensure block.

Disabling ActiveRecord::Timestamp from updating the updated_at , columns, automatic timestamping, 213 updated_at timestamp field, touching columns, automatic timestamping, 213 updates responding to page, 679–680 87–88 AssetUrlHelper, 400–406 avoid numeric IDs in, 63 direct routes linking to​  This Rails plugin extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes. 37 commits.

The Rails 5 Way, On the other hand, update_attributes is subject to validation checks and is If you have associations on a model, Active Record automatically creates Definitely consider using attr_accessible to prevent mass assignment whenever there's a with no arguments updates the updated_at timestamp field to the current time  Ruby on Rails. Contribute to rails/rails development by creating an account on GitHub.

The Rails 3 Way, Have you ever had the need to keep track of when a record was created or when named fields within the schema and populate them with data automatically if On the flipside, whenever you edit and update that record, the current timestamp We'll close out this chapter with a final bit of advice for how to deal with oddly  It's mostly just for keeping track of my employee's information like phone numbers, drivers license expiration, etc. Since I update it so frequently, I want an automatic timestamp that rests at the top or bottom of my worksheet that will update with today's date everytime I update information ANYWHERE on the spreadsheet.

Foundation Rails 2, :float Don't use floats to store currency values or, more accurately, any type of data You should keep in mind that you won't get an error if you try to store values will automatically timestamp create operations if the table has columns named The same applies to updates when there are columns named updated_at or  The TIMESTAMP and DATETIME data types offer automatic initialization and updating to the current date and time. For more information, see Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

Comments
  • You can even pull this into a method, e.g. def without_timestamps old = ActiveRecord::Base.record_timestamps ActiveRecord::Base.record_timestamps = false begin yield ensure ActiveRecord::Base.record_timestamps = old end end
  • Do we need to reset it back to true even when executing scrips or code inside the console ?
  • I think stackoverflow.com/a/37193298/1214748 is much better. It doesn't turn it off globally for the whole application
  • @maschwenk true, for rails 5. but for those forced to use an older version of rails, this still seems the best option.
  • Mind that for serialized columns you will have to yaml the value yourself beforehand.
  • This by far is the cleanest way to move the records.
  • Seems a little odd that the same option doesn't work when doing update. You need to split it into two operations as shown here.
  • Just a warning, this is not threadsafe. Recently got bitten by this!