Update Rails model without touching timestamp temporary -
i have tweet
model. want update model attributes without updating timestamp.
i've read/tried links , didn't work me:
- how update single attribute without touch updated_at attribute?
- http://blog.bigbinary.com/2009/01/21/override-automatic-timestamp-in-activerecord-rails.html
i tried override activerecord::base
mentioned in second link, creating new file active_record_base_patches.rb
inside lib
directory. but, engine said method not found in tweet
model.
so, tried move update_record_without_timestamping
tweet
model. method found timestamp updated.
is there simple method disable timestamp in rails while? laravel 4 has this: https://stackoverflow.com/a/18906324/3427434.
ps: use ruby on rails 4.2.3 on ruby 2.2.0.
you create concern in model/concern
, named model_timestamp.rb
require 'active_support/concern' module modeltimestamp extend activesupport::concern module classmethods def without_timestamps old = activerecord::base.record_timestamps activerecord::base.record_timestamps = false begin yield ensure activerecord::base.record_timestamps = old end end end end
then in model tweet
add include modeltimestamp
class tweet < activerecord::base include modeltimestamp ... end
then want update attributes without timestamps
change use
tweet = tweet.last tweet.without_timestamps tweet.update(attributes) end
Comments
Post a Comment