Migrations: determine migration number in self.up()?


#1

Hi all

I need to know the number of a migration within the self.up() method,
and I DON’T WANT TO RELY ON THE FILENAME (XXX_my_migration.rb).

So is there a way to determine it somehow?

def self.up
puts 'doing migration number ’ + migration_number()
end

Thanks a lot for help.
Josh


#2

Hi Joshua,

I need to know the number of a migration within the
self.up() method, and I DON’T WANT TO RELY ON
THE FILENAME (XXX_my_migration.rb).

So is there a way to determine it somehow?

def self.up
puts 'doing migration number ’ + migration_number()
end

Not so nice, but you can try that :

def self.up
puts “doing migration number :
#{::ActiveRecord::Migrator.current_version}”
end

– Jean-François.


Ruby ( http://www.rubyfrance.org ) on Rails ( http://www.railsfrance.org
)


#3

Thanks a lot!

May I ask what the prepended :: means and how it works?

::ActiveRecord::Migrator.current_version

I remarked that the returned number does NOT correspond with the file
name’s number. Is this because the file number itself is only used for
sorting the migrations in ascending order and not because it is
recognized as a number itself?

Thanks.


#4

Another question:

migration_number = ::ActiveRecord::Migrator.current_version.to_i

gives me an error:

String can’t be coerced into Fixnum

What can I do? The output is “0” (zero), so why is there a problem?


#5

Joshua M. wrote:

Another question:

migration_number = ::ActiveRecord::Migrator.current_version.to_i

gives me an error:

String can’t be coerced into Fixnum

What can I do? The output is “0” (zero), so why is there a problem?

Oh, I got this one myself. :wink: