Renaming the created_at, updated_at columns of ActiveRecord/Rails
This can be done via just over writing the ActiveRecord::Timestamp module methods. Not actually overwriting the entire module. I need to to accomplish this same goal as I am working with a legacy database. I am on rails 3 and I have started following a methodology of how to monkey patch my code with over writing rails functionality.
I first create the base project that I am working with and create a file called that in the initializers. In this case I created active_record.rb. And within the file I put code to override two methods that controlled the timestamp. Below is the sample of my code:
module ActiveRecord
module Timestamp
private
def timestamp_attributes_for_update #:nodoc:
["updated_at", "updated_on", "modified_at"]
end
def timestamp_attributes_for_create #:nodoc:
["created_at", "created_on"]
end
end
end
NOTE: I would also like to mention that this sort of monkey patching to get things to work is frowned upon and may break on upgrades so be careful and be fully aware of what it is that you want to do.
Updates:
- Change timestamp column names from symbol to string per api change. Thanks Techbrunch for bringing this API change to my attention.
I think NPatel's method is a right approach, but it is doing too much if you only need to change #created_at on a single model. Since the Timestamp module is included in every AR object, you can just override it per model, not globally.
<= Rails 5.0
class User < ActiveRecord::Base
...
private
def timestamp_attributes_for_create
super << :registered_at
end
end
Rails ~> 5.1
private_class_method
def self.timestamp_attributes_for_create
# only strings allowed here, symbols won't work, see below commit for more details
# https://github.com/rails/rails/commit/2b5dacb43dd92e98e1fd240a80c2a540ed380257
super << 'registered_at'
end
end
An updated anwser for Rails >= 5.1. I would suggest to use the ApplicationRecord
that is available by default in your application and define the following:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
class << self
private
def timestamp_attributes_for_create
super << 'my_created_at_column'
end
def timestamp_attributes_for_update
super << 'my_updated_at_column'
end
end
end
Note that you can also use this block on a specific model if you don't want to configure it for all your models.
Note also that you should use strings and not symbols.