RVM environment variable - ruby-on-rails

For my Ruby on Rails project I have to do a lot of stuff in the command line, like running rake tasks or running test specs. In my app I need to supply an environment variable to do this, e.g. ENCRYPTION_PASSWORD=bla rake db:test:prepare
What is a good way to add environment variables to RVM, so I don't have to use the variable every single time in the command line? Thanks!

In your applications folder you can place a .rvmrc file with the settings you need for your project. This file is read, when you change to this directory. E.g, I have a .rvmrc with this content:
rvm use ruby-1.9.2-p320
couchdb -b
When I enter the applications folder, rvm is firing a hook with parsing the file contents:
cd project/
Using /Users/andwen/.rvm/gems/ruby-1.9.2-p320
Running /Users/andwen/.rvm/hooks/after_use
Apache CouchDB is running as process 39539, time to relax.
Running /Users/andwen/.rvm/hooks/after_cd

Related

How to execute "bin/setup" in rails on Windows?

As far as I know bin/setup is for easy set up for every project in rails e.g installing required gems, preparing database etc. But how to do this in Windows environment?
You'll need ruby installed first.
Open a command prompt in the relevant folder and run:
ruby bin/setup

Accessing two different Rails environments from a single Ruby script

I'm trying to write a Rake task that will retrieve data stored in the databases of two different Rails apps, with different Ruby and gem versions. What I have so far is
task :get_data do
puts `/<path>/<to>/<first>/<app>/bin/rails runner 'FirstDataRetriever.new.as_set'`
puts `/<path>/<to>/<second>/<app>/bin/rails runner 'SecondDataRetriever.new.as_set'`
end
The problem is that Rails runner is trying to execute the DataRetriever classes using the Ruby version of the Rakefile, which is in a separate repo using different infrastructure.
Both the DataRetriever classes make use of ActiveRecord models in each app, so they must be run in the Rails environment of each app, but I'm not sure how to go about this.
You can specify Ruby version in .ruby-version file inside of root directory of your projects if you are using RVM or rbenv.
You can specify Rails environment use RAILS_ENV environment variable i.e. RAILS_ENV=production bin/rails runner ...
I hope it can help to solve your problems.

What's inside my rails ./bin directory?

Ruby on Rails 4 introduced* the ./bin directory. Inside of ./bin are a few executables: bundle, rails, rake, setup, spring.
My questions are:
What do these executables do? Why do they exist?
When are they invoked?
Can I edit them? Why would I edit them?
I've read the source of these files, and I've tried to Google their purpose, but I can't wrap my head around it. I'm looking for an in-depth explanation.
*I believe many of these files existed before Rails 4, just in different location.
Introduced in Rails 4, the ./bin directory contains your app's "binstubs." Binstubs are wrappers around gem executables, like rails or bundle, which ensures a gem executable is run inside the correct environment for your Rails app.
Binstubs can be used in lieu of bundle exec to run a gem's executable inside your app's environment. For example, instead of typing bundle exec rails scaffold products you can type bin/rails scaffold products. Using binstubs is more flexible than bundle exec, because you don't have to cd to the app's root directory and type bundle exec before everything.
By default, bundle, rails, rake, setup, spring binstubs are created automatically for new rails projects. To make a binstub for a gem executable, just type bundle binstubs name_of_gem_executable. You'll find the new binstub in your ./bin directory.
Some suggest putting ./bin in your shell's search $PATH, so that you don't have to type bin/rails and you can just type rails. This is risky because it depends on ./bin coming before the gem executable's path in $PATH; if you happen to forget this ordering and adjust $PATH such that the gem's executable is found before the binstub wrapper, you could easily invoke the gem's executable -- sans the environmental pretext -- without realizing it.
A bin (short for binary) is nothing more than an application. As you have noticed, these files are ruby files, but they do not have the .rb extension and can be run from your shell just as any shell command, without the need to start any ruby interpreter yourself.
So what do theses programs do? I'm pretty sure you know already what rails rake bundle do.
About spring, it's a gem that keeps your app running in the background (hence its need to be run independently from the app). More infos on their github.
I see no reason to edit these files, but that being said, they're ruby files so you can do whatever you want with them. One example of why you may want editing can be found here.
I personally do put some stuffs in the bin folder. Scripts to connect to remote servers, or ruby scripts I need but that I don't want to run as rake tasks since they're more general than my application.

bin/rails giving error that 'bin' is not recognized as internal

I am a beginner trying to learn and do the Ruby on Rails tutorials (http://guides.rubyonrails.org/getting_started.html#installing-rails)
and for all the bin commands in the command window such as
$ bin/rails --version
or
$ bin/rails server
I am getting the error " 'bin' is not recognized as an internal or external command, operable program or batch file.'
I just installed the latest version of Ruby so I've tried running
rake rails:update:bin
and I also added the PATH variable C:\RailsInstaller\Ruby1.9.3\bin to my environment variables.
I'm still getting the same error.
Thank you in advance!
You are getting those errors because you are on Windows. In this case you have to pass the scripts under the bin folder directly to the Ruby interpreter like this:
ruby bin\rails server
If you have added the bin directory to your PATH variable, then you don't need to use it in the command. Just type $ rails --version, etc.

Capistrano user-related problem

I'm deploying a rails application using Capistrano to a shared server (on WebFaction). This means there are several different Ruby version installed there.
Everything works fine until I want to run some rake tasks. These tasks fail by saying I don't have the right RubyGems version.
From this thread I understood that fixing it would be as easy as adding the full path to the correct rake folder to my rake commands. I tried that but failed again, now because the Ruby interpreter that is used is the wrong one.
I assumed that capistrano would use the username/password I gave it to run commands on the server but it seems not to be the case.
Am I missing something? How can I fix that?
Thanks!
This is indeed a path issue. I solved it by symlinking my ruby, rake, and rails executables to my path. For instance, if the executables that you want (but are failing to work) are located at /opt/ruby/bin/ruby, you'll symlink it to:
ln -s /opt/ruby/bin/ruby /usr/local/bin/ruby

Resources