Getting started with Ruby on RailsRoutingActiveRecordViewsActiveRecord MigrationsRails Best PracticesNaming ConventionsActionCableActiveModelUser Authentication in RailsActiveRecord AssociationsActiveRecord ValidationsActiveRecord Query InterfaceActionMailerRails generate commandsConfigurationI18n - InternationalizationUsing GoogleMaps with RailsFile UploadsCachingActionControllerConfigurationSafe ConstantizeRails 5Authorization with CanCanMongoidGemsChange default timezoneAsset PipelineUpgrading RailsActiveRecord LockingDebuggingConfigure Angular with RailsRails loggerPrawn PDFRails APIDeploying a Rails app on HerokuActiveSupportForm HelpersActiveRecord TransactionsRSpec and Ruby on RailsDecorator patternElasticsearchReact with Rails using react-rails gemRails Cookbook - Advanced rails recipes/learnings and coding techniquesMultipurpose ActiveRecord columnsClass OrganizationShallow RoutingModel states: AASMRails 5 API AutheticationTesting Rails ApplicationsActive JobsRails frameworks over the yearsAdd Admin PanelNested form in Ruby on RailsFactory GirlImport whole CSV files from specific folderTools for Ruby on Rails code optimization and cleanupActiveJobActive Model SerializersRails Engine - Modular RailsSingle Table InheritanceActiveRecord TransactionsTurbolinksFriendly IDSecurely storing authentication keysAuthenticate Api using DeviseIntegrating React.js with Rails Using HyperloopChange a default Rails application enviornmentReserved WordsRails -EnginesAdding an Amazon RDS to your rails applicationPayment feature in railsRails on docker

Upgrading Rails

Other topics

Upgrading from Rails 4.2 to Rails 5.0

Note: Before upgrading your Rails app, always make sure to save your code on a version control system, such as Git.


To upgrade from Rails 4.2 to Rails 5.0, you must be using Ruby 2.2.2 or newer. After upgrading your Ruby version if required, go to your Gemfile and change the line:

gem 'rails', '4.2.X'

to:

gem 'rails', '~> 5.0.0'

and on the command line run:

$ bundle update

Now run the update task using the command:

$ rake rails:update

This will help you to update configuration files. You will be prompted to overwrite files and you have several options to input:

  • Y – yes, overwrite
  • n – no, do not overwrite
  • a – all, overwrite this and all others
  • q – quit, abort
  • d – diff, show the differences between the old and the new
  • h – help

Typically, you should check the differences between the old and new files to make sure you aren't getting any unwanted changes.

Rails 5.0 ActiveRecord models inherit from ApplicationRecord, rather than ActiveRecord::Base. ApplicationRecord is the superclass for all models, similar to how ApplicationController is the superclass for controllers. To account for this new way in which models are handled, you must create a file in your app/models/ folder called application_record.rb and then edit that file's contents to be:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

Rails 5.0 also handles callbacks slightly different. Callbacks that return false won't halt the callback chain, which means subsequent callbacks will still run, unlike Rails 4.2. When you upgrade, the Rails 4.2 behavior will remain, though you can switch to the Rails 5.0 behavior by adding:

ActiveSupport.halt_callback_chains_on_return_false = false

to the config/application.rb file. You can explicitly halt the callback chain by calling throw(:abort).

In Rails 5.0, ActiveJob will inherit from ApplicationJob, rather than ActiveJob::Base like in Rails 4.2. To upgrade to Rails 5.0, create a file called application_job.rb in the app/jobs/ folder. Edit that file's contents to be:

class ApplicationJob < ActiveJob::Base
end

Then, you must change all of your jobs to inherit from ApplicationJob rather than ActiveJob::Base.

One of the other biggest changes of Rails 5.0 doesn't require any code changes, but will change the way you use the command line with your Rails apps. You will be able to use bin/rails, or just rails, to run tasks and tests. For example, instead of using $ rake db:migrate, you can now do $ rails db:migrate. If you run $ bin/rails, you can view all the available commands. Note that many of the tasks that can now be run with bin/rails still work using rake.

Contributors

Topic Id: 3496

Example Ids: 12067

This site is not affiliated with any of the contributors.