Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Using migrations to migrate data from one table into another

Rails • Asked by Shairyar Baig

I have two models user and account linked with each other as following

belongs_to :user

has_many :accounts

I want to move the data from column name in users table into column name in accounts table

I think the following should do the job but my question is if there is a better way of doing this inside a migration file considering if there are thousands of records?

Account.find_each do |account|
      if account.user.present?
        account.update_columns(name:, xyz:

Note: Once this migration will run I will remove the column user_id in accounts table removing the relationship between the two modals.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 29,763+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.