views:

1415

answers:

4

I want to rename the timestamp columns defined in timestamp.rb . Can the methods of timestamp.rb be overwritten? And what has to be done in the application that the module with the overwritten methods is used.

+3  A: 

There is no simple way to do that. You can accomplish this either by overriding the ActiveRecord::Timestamp module, or by writing your own one to do the magic for you.

Here is how the magic works.

Milan Novota
+3  A: 
Maximiliano Guzman
Thanks, this is a good solution.
RainerB
A: 

Or you could just make your own columns (DateTime) and just manually set the created_at column when you create and set the updated_at column when you update. That might be easier than the hack above. That's what I'm gonna do. Better yet, update rails to allow us to change these names.

MattDiPasquale
+1  A: 

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

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.

NPatel