Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

352
Vistas
Rails: How to modify data using migrations due to change in the schema

I have following two migrations:

One, Add column contextual_page_number to transcripts table:

class AddContextualPageNumberToTranscripts < ActiveRecord::Migration[5.2]
  def change
    add_column :transcripts, :contextual_page_number, :integer, default: 1
  end
end

Second, changing the value of the previous added column contextual_page_number based on value of another column:

class ChangePageOffsetAndContextualPageNumberOfTranscripts < ActiveRecord::Migration[5.2]
  def up
    Firm.all.find_in_batches do |group|
      group.each do |firm|
        Apartment::Tenant.switch(firm.tenant) do
          Transcript.where.not(page_offset: 0).each do |transcript|
            transcript.update(
              contextual_page_number: ((transcript.page_offset - 1) * -1),
              page_offset: 1
            )
          end
        end
      end
    end
  end

  def down
    ..
  end
end

After running the migration, I am getting unknown attribute contextual_page_number error.

== 20211108132509 AddContextualPageNumberToTranscripts: migrating ============= -- add_column(:transcripts, :contextual_page_number, :integer, {:default=>1}) -> 0.0095s == 20211108132509 AddContextualPageNumberToTranscripts: migrated (0.0096s) ====

== 20220113095658 ChangePageOffsetAndContextualPageNumberOfTranscripts: migrating rails aborted! StandardError: An error has occurred, this and all later migrations canceled:

unknown attribute 'contextual_page_number' for Transcript.

I have even tried reset_column_information, but no luck:

Apartment::Tenant.switch(firm.tenant) do
  Transcript.connection.schema_cache.clear!
  Transcript.reset_column_information
  ..
end

Any clue would be of great help, thanks.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

As mentioned in one of the answer, I tried reset_column_information just right after the add_column, but that didn't worked. Finally, SQL to the rescue..

sql_cmd = "UPDATE transcripts
           SET contextual_page_number = ((page_offset - 1) * -1),
               page_offset = 1
           WHERE page_offset != 0"

Transcript.connection.execute(sql_cmd)
over 4 years ago · Santiago Trujillo Denunciar

0

You need two migration files. First, try running the migration and check schema.rb for the table transcripts and verify that the newly added column contextual_page_number is being added or not.

Once you are sure that your new column is added, then again create a new migration like, eg: MigrateTransriptsCloningsData, and then add the desired changes in the up block, then execute db:migrate to update the required changes.

My choice would be To add a new rake task and executing it. like bundle exec rake migrate_transcripts_data:start instead of keeping that logic in the db/migrate/your_new_migration_file, choice is yours.

over 4 years ago · Santiago Trujillo Denunciar

0

reset_column_information should be the correct way to resolve this sort of problem if you want to use models in a migration. This isn't without its problems though.

I suspect the issue is that you are calling it too late somehow. Put it first thing in the up method of the second migration or after the add_column in the first migration.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda