How to update a single attribute without touching the updated_at attribute?
Is there a way to avoid automatically updating Rails timestamp fields?
Or closer to your question:
http://blog.bigbinary.com/2009/01/21/override-automatic-timestamp-in-activerecord-rails.html
Upsert with attribute included updated_at
FirstOrCreate()
calls Updates()
when data exist with given ID
.Updates()
operation will perform the model's BeforeUpdate
, AfterUpdate
method, update its UpdatedAt
timestamp, save its Associations when updaing, if you don't want to call them, you could use UpdateColumn
, UpdateColumns
with separate Create
operation when data is not exist.
Ref: Gorm official document about update
how to update the field 'updated_at' of a sequelize model without modifiyng any attributes
To update an instance value, you can use instance.set(key, value, [options]).
myInstance.set('updatedAt', new Date());
myInstance.save().then(function() {
// my nice callback stuff
});
The sequelize docs have a good example of doing an update query if you don't have an instance:
Post.update({
updatedAt: null,
}, {
where: {
deletedAt: {
$ne: null
}
}
});
How to prevent gorm from ignoring values for the `UpdatedAt` field in the .Updates() clause?
There is a way.
Gorm's Update/Updates
method will ignore any value you pass in for UpdatedAt and use the current timestamp instead. This is by design.
You can get gorm to not update the UpdatedAt field by using Omit("UpdatedAt")
. However, when you do that the database will still set the updated_at
column because of on update current_timestamp
.
The way to get around this would be to use the UpdateColumns
method which explicitly does not do time tracking:
err := db.Model(&user).
Omit("user_id").
UpdateColumns(User{
LastLogin: lastLogin,
UpdatedAt: found.UpdatedAt,
}).
Error
// error check
Overall I would consider dropping that on update
clause since it's interfering with/duplicating what Gorm is doing.
Update without touching timestamps (Laravel)
Disable it temporarily:
$user = User::find(1);
$user->timestamps = false;
$user->age = 72;
$user->save();
You can optionally re-enable them after saving.
This is a Laravel 4 and 5 only feature and does not apply to Laravel 3.
how to prevent updating of updatedAt field while saving or updating documents in mongodb?
updatedAt field is updated when you use update query of mongoose. if we directly use $set query it will only update the mentioned fields inside $ set query. The reason is updatedAt field is managed by mongoose not by mongodb.
Is there a way to avoid automatically updating Rails timestamp fields?
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
Can I (and should I) override the createdAt and updatedAt properties of a Sails.js model?
In your model, you can turn off the defaults.
autoCreatedAt: false,
autoUpdatedAt: false,
You are now free to use your own values. You can use the beforeUpdate lifecycle callback (if needed) to add any extra behaviour just prior to updating the model.
For example:
beforeUpdate:function(values, next) {
// do whatever you need here if required.
// you could just override the default createdAt, updatedAt behaviour
next();
}
See similar question answered by a core maintainer here.
If this still doesn't quite address your needs, then leave me a comment below and I will try to update the answer further.
Sequelize: updating updatedAt manually
This worked for me
group.changed('updatedAt', true)
await group.update({
updatedAt: new Date()
})
Calling just update with updatedAt = new Date is not enough, you must flag column as changed
Related Topics
Adding a Submit Button Image to a Rails Form
Rake Db:Migrate Error with MySQL2 Gem - Library Not Loaded: Libssl.1.0.0.Dylib
Ruby/Ror: Calling Original Method via Super()
In Ruby, How to I Control the Order in Which Test::Unit Tests Are Run
How to Uppercase Each Element of an Array
How to Lazily Evaluate an Arbitrary Variable with Chef
How to Loop Through a Daterange with Different Intervals
How to Get an Empty Temporary Directory in Ruby on Rails
What Is the Best Diff Library in Ruby
Is There a Short Way to Write '{|X| X}'
Stylesheet_Link_Tag :All Versus :Media =>All
Get Server File Path with Paperclip