Run Rails commands outside of console
There are two main ways to run commands outside console:
Rake task which depends on :environment
rails runner (previously script/runner), eg:
$ rails runner "query"
Both are pretty well documented on the rails guide: https://guides.rubyonrails.org/command_line.html#bin-rails-runner
Both of these methods will still take the same time as a console to fire up, but they are useful for non-interactive tasks.
How to run some default commands after firing the Rails console?
Rails' console is IRB. IRB supports an .irbrc file, which contains initialization information and settings.
Read "https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick" and "My .irbrc for console/irb" for ideas.
How to run rails commands in production with capistrano 3 and rvm installed
Solved with the help of this Upwork freelancer.
The solution steps were:
- remove the binstubs locally
set :bundle_binstubs, nil
inconfig/deploy.rb
- remove the bin directory from the
:linked_dirs
list (adding also/bin
in.gitignore
) - push the changes and run
cap production deploy
- recreate the binstubs with
rake rails:update:bin
- comment out the
set :bundle_binstubs, nil
line - add the bin directory in
:linked_dirs
again - modify the
config/deploy.rb
file like this:
namespace :deploy do
task :regenerate_bins do
on roles(:web) do
within release_path do
execute :bundle, 'exec rake rails:update:bin'
end
end
end
...
...
after :finishing, :regenerate_bins
...
- uncomment
set :bundle_binstubs, nil
and remove bin from:linked_dirs
once more - push changes and deploy
After this, the binstubs are found in the current/bin
directory instead of the shared/bin
one (in Rails 4 and 5)
Pass ruby script file to rails console
In the meantime, this solution has been supported.
rails r PATH_TO_RUBY_FILE
Much simpler now.
Run command on Rails Console startup
You can write ruby code in irb
config file. Edit the .irbrc
file and add your ruby code used to determine whether you connected to local or remote
if defined? Rails
//ruby code to find you connected to local or remote
puts "USING REMOTE DATABASE...."
end
rails console command not working
I have figured it out.
I just remove bin directory and run the command to regenerate bin again.
Running below commands solve my problem.
rm -r bin/*
bundle exec rake rails:update:bin
Related Topics
Rendering Haml Partials from Within Haml Outside of Rails
Printing to Screen in a Rake Task
How to Get a List of All Available Rake Tasks in a Namespace
Ruby: How to Store and Display a Day of the Week
How to Do Attr_Accessor_With_Default in Ruby
Getting Devise Sign_In Form into Twitter Bootstrap Modal
Synchronized Method for Concurrency in Ruby
Remove "Www", "Http://" from String
Is It Acceptable Practice to Patch Ruby's Base Classes, Such as Fixnum
After Installing Ruby Gems, Running the New Gem Returns "Could Not Find" Errors
What Are the Conventional Gem Paths for Ruby Under Os X 10.5
How to Validate a Specific Attribute on an Activerecord Without Instantiating an Object First
What Does Class Classname < ::Otherclassname Do in Ruby
Rails, Activerecord: Self[:Attribute] VS Self.Attribute