Forum: Ruby on Rails Migrations: determine migration number in self.up()?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-09 12:17
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
A99870c1391c39da2089649745965bda?d=identicon&s=25 Jean-François Trân (Guest)
on 2007-04-09 12:27
(Received via mailing list)
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
)
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-09 12:41
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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-09 12:48
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?
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-09 12:59
Joshua Muheim 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. ;-)
This topic is locked and can not be replied to.