This blog is part of our Rails 5 series.
In Rails 4 some commands start with rails and some commands start with rake. This could be quite confusing for people new to Rails. Let's see an example.
Our task is to write a database migration and then to run that migration.
1 2rails g migration create_users 3
Above command creates a migration. Now we need to run that migration.
1 2rake db:migrate 3
As you can see first command starts with rails and second command starts with rake.
In order to consolidate them we can either use rails for everything or we can use rake for everything.
Choosing rails command over rake command
Some favor using rake over rails. But an important feature missing in Rake is the ability to pass arguments.
1 2rails console development 3
In order to execute the above command using rake we will have to pass console and development as arguments. We can pass these values using environment variables. That would mean adding additional code in Rake task to fetch right values and then only we will be able to invoke command rails console development.
Rails 5 enables executing rake commands with rails
Rails core team decided to have consistency by enabling rails command to support everything that rake does.
For example in Rails 5 commands like db:migrate, setup, test etc which are part of rake command in Rails 4 are now being supported by rails command. However you can still choose to use rake to run those commands similar to how they were run in Rails 4. This is because Rails community has introduced Rake Proxy instead of completely moving the command options from rake to rails.
What happens internally is that when rails db:migrate command is executed, Rails checks if db:migrate is something that rails natively supports or not. In this case db:migrate is not natively supported by rails, so Rails delegates the execution to Rake via Rake Proxy.
If you want to see all the commands that is supported by rails in Rails 5 then you can get a long list of options by executing rails --help.
Use app namespace for framework tasks in Rails 5
As rails command is now preferred over rake command, few rails namespaced framework tasks started looking little odd.
1 2$ rails rails:update 3 4$ rails rails:template 5 6$ rails rails:templates:copy 7 8$ rails rails:update:configs 9 10$ rails rails:update:bin 11
So, Rails team decided to change the namespace for these tasks from rails to app.
1 2$ rails app:update 3 4$ rails app:template 5 6$ rails app:templates:copy 7 8$ rails app:update:configs 9 10$ rails app:update:bin 11
Using rails rails:update will now give deprecation warning like: DEPRECATION WARNING: Running update with the rails: namespace is deprecated in favor of app: namespace. Run bin/rails app:update instead.
More improvements in pipeline
In Rails 4, the routes are usually searched like this.
1 2$ rake routes | grep pattern 3
There is an effort underway to have a Rails command which might work as shown below.
1 2$ rails routes -g pattern 3
There is also an effort to enable lookup for controller like this.
1 2$ rails routes -c some_controller 3