Pass ruby script file to rails console
In the meantime, this solution has been supported.
rails r PATH_TO_RUBY_FILE
Much simpler now.
Loading Rails environment and classes from an independent gem
I think this is the proper way to load a Rails app (found in config.ru): require ::File.expand_path('../config/environment', __FILE__)
. In fact you can copy and paste that into a regular irb session to turn it into a 'rails console' session.
So the key seems to be to require your config/environment
and that should allow you to use your app models normally.
Run Rails Runner from Ruby Script
What you want to do is write a Rake task instead:
# lib/tasks/foo.rake
namespace :foo do
description "@TODO write a descripion"
task bar: :environment do
# your logic goes here
puts [Rails.env, ApplicationController::CONSTANT_NAME]
end
end
This task can be invoked via bin/rake foo:bar
. bar: :environment
loads the Rails environment for this task.
This is a lot less hacky/wonky then using the rails runner, and is the defacto way of writing tasks/scripts in Ruby that are meant to invoked from the command line.
How can I set the Rails environment for my somewhat stand alone Ruby script?
If you're going to be using the rails environment, your best bet would be to make this a rake script. To do this, put a twitter.rake file into lib/tasks and begin and end it like this:
task(:twitter_load => :environment) do
# your code goes here
end
That way, you're doing it by "following conventions" and it doesn't have that 'orrible smell associated with it.
How do I run scratch file in RubyMine or IDEA in Rails environment?
I've found two ways to do it, depending on your workflow one might fit you better than another. Assuming you're using IDEA 14 (it might be different for earlier versions) and Rails 4.1+.
Custom runner
- In top menu
Run -> Edit Configurations...
; - Configuration tab:
2.1. Ruby script:<Path to your bin/rails file>
;
2.2. Script arguments:runner <Path to your script>
;
2.3. Working directory:<Your project dir>
;
2.4. Environment variables:RAILS_ENV=development
;
2.5. Ruby arguments:-e '$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)'
;
2.6. Ruby SDK:<Your project SDK>
. - Bundler tab:
3.1. Run the script in context of the bundle (bundle exec):<check>
.
The problem with this approach is that you would have to manually change path to scratch file each time you want to run the different one (please comment if you know workaround).
External tool
Assuming you have Spring installed:
- In top menu
IntelliJ IDEA -> Preferences
; Tools -> External Tools
;- Hit
+
in the bottom of the menu:
3.1. Name:Rails Runner
;
3.2. Program:$ProjectFileDir$/bin/spring
;
3.3. Parameters:rails runner $FilePath$
;
3.4. Working directory:$ProjectFileDir$
. Keymap
:
4.1. Search forRails Runner
;
4.2. Double-click and add custom shortcut (Alt + S
is convenient and available).
The only problem with this approach is that you have to have custom shortcut to make it convenient.
Related Topics
Rails Unable to Convert Unpermitted Parameters to Hash
How to Add to a Serialized Array
Combine Array of Array into All Possible Combinations, Forward Only, in Ruby
How to Enable Tls V1.2 in Ruby? If So, How
How to Easily Parse a Url with Parameters in a Rails Test
How to Ftp in Ruby Without First Saving the Text File
How to Test Exception Raising in Rails/Rspec
How to Set Private Instance Variable Used Within a Method Test
Rails 3.1 Absolute Url to an Image
How to Write a Ruby Switch Statement (Case...When) with Regex and Backreferences
Format the Date Using Ruby on Rails
Add Image to Layout in Ruby on Rails