Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:58:36+00:00 2026-05-28T02:58:36+00:00

My table has a column order that I want initially set to equal the

  • 0

My table has a column “order” that I want initially set to equal the record’s ID.

Can I do this in the migration, or do I need to set it in the create action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:58:36+00:00Added an answer on May 28, 2026 at 2:58 am

    If you already have data, you can’t do this in a create callback action because those items have already been created. However, a create callback would help you for new objects you will create:

    after_create :set_order_attribute
    
    def set_order_attribute
      self.update_columns(order: id) if order.nil?
    end
    

    You can technically update existing data in a migration as a SQL statement, but I wouldn’t recommend using commands involving the ActiveRecord model as the migration may break sometime down the line as the model changes. To do this in a migration you would have an execute command in the :up method (not sure what behavior you would want for :down) like so:

    def up
      table_name = "foo"
      execute "UPDATE #{table_name} SET order = id;"
    end
    

    Alternatively you could create a separate rake task to do the update; this would keep your migrations cleaner and give you better control over when you will run the update task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My DB table has a column that can contain a very big amount of
If I have a table that has a TIMESTAMP column, and I want to
I have a table that has a START_DATE and an END_DATE column. I want
I have a table that has a column that has the cost of a
i have a table that has following column Type -------- type 1 type 2
I have mysql table that has a column that stores xml as a string.
I'm trying to query a table which has a column that contains straight xml
I have a table named Orders that has a column named OrderCode that stores
I have this query statement and want to only get records that has a
My table has many row that have a same column. I'll call the column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.