american-ninja-warrior
american-ninja-warrior

Reputation: 8175

rails console OR irb find out the RAILS_VERSION of the current environment

Somewhat related to: Determine ruby version from within Rails

How to find out the RAILS_VERSION from within rails console?

Upvotes: 5

Views: 1864

Answers (2)

BoP
BoP

Reputation: 457

One of possible ways is:

pry(main)> Rails.version
=> "5.1.7"

or like above was suggested:

pry(main)> Rails.gem_version
=> Gem::Version.new("5.1.7")

Then you can build some conditions using this technique of versions comparisons:

[20] pry(main)> Rails.version.starts_with?('5.1')
=> true
[21] pry(main)> Gem::Version.new(Rails.version) > Gem::Version.new('5.1.2')
=> true
[22] pry(main)> Gem::Version.new(Rails.version) > Gem::Version.new('5.2.3')
=> false
[23] pry(main)> Gem::Version.new(Rails.version) == Gem::Version.new('5.1')
=> false
[24] pry(main)> Gem::Version.new(Rails.version) == Gem::Version.new('5.1.7')
=> true

OR

[28] pry(main)> Rails.gem_version == Gem::Version.new('5.1.7')
=> true
[29] pry(main)> Rails.gem_version >= Gem::Version.new('5.1.2')
=> true
[30] pry(main)> Rails.gem_version < Gem::Version.new('5.2.2')
=> true

Upvotes: 2

mrzasa
mrzasa

Reputation: 23307

Short info in Rails.gem_version:

Rails.gem_version
# => Gem::Version.new("6.0.1")

Long info in Rails::Info:

Rails::Info
=> About your application's environment
Rails version             6.0.1
Ruby version              ruby 2....
RubyGems version          2.7...
Rack version              2.0...
Middleware                UTF8Cleaner::Middleware, Rack::Cors, ActionDispatch::HostAuthorization, Rack::Sendfile, ActionDispatch::Static, Rack::Lock, ActionDispatch::Executor, ...
Application root          /home/aaa/bb/app
Environment               development
Database adapter          mysql
Database schema version   20195201212345

Upvotes: 8

Related Questions