Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to avoid automatically updating rails timestamp fields ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 561
    Comment on it

    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 chances that we have to update the columns without touching these columns.

    You can do this anywhere like in migration or in a rake task:

    ActiveRecord::Base.record_timestamps = false
    begin
      importing_data_code
    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 can also work on the individual models, e.g.

    Person.record_timestamps = false
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: