How do I ensure my jruby command line options are used when running "rails", "rake", "rspec" etc? - ruby-on-rails-3

I currently run my Rails app using:
jruby --1.9 -J-XX:+CMSClassUnloadingEnabled -J-XX:+UseConcMarkSweepGC -J-XX:MaxPermSize=256m -S rails server
This is getting pretty old now. How can I set my Rails project up so that just running
rails server
has the same effect?
(Note: bash aliases and the like are not what I'm looking for here. I want to make the project work right, not fix my local settings)

When using RVM and a project .rvmrc, the canonical way is to set PROJECT_JRUBY_OPTS in the project .rvmrc. A bug prevented this from working for me, so use rvm head.
If not using rvm then use JRUBY_OPTS, which is the built-in way of doing it that JRuby checks (in fact, the PROJECT_JRUBY_OPTS thing ends up being converted to JRUBY_OPTS by rvm).

Related

Configure RVM to print notice when using .ruby-version and .ruby-gemset

When cding into a directory containig an .rvmrc, RVM usually prompts the user about the change in ruby/gemset. For example:
Using /Users/USERNAME/.rvm/gems/ruby-2.2.1 with gemset GEMSETNAME
However, when using the preferred .ruby-version and .ruby-gemset, RVM does not print this notice, and silently changes the ruby version and gemset.
How can one configure RVM to print the typical ruby/gemset switching message when only a .ruby-version and .ruby-gemset exist in a project directory? The similar question "Not getting info message after converting rvm from .rvmrc to .ruby-version" specifies a solution that requires the presence of .rvmrc. Doing something similar with a $HOME/.rvmrc is close, but this question is asking how to get the notice without this approach, which feels like a bit of a hack.
Try this :
echo rvm_use_flag=2 >> ~/.rvmrc

How to create an RVM environment on a server through net-ssh?

I have a script which is installing Ruby/RVM (and more) on my server to get an environment ready to deploy a Rails project. This script is in ruby and use the net-ssh lib to do the job.
After having installed RVM, Ruby, I would like to create the project Gemset:
connection do |conn|
logger("Create RVM environment #{ruby_version}##{project_name}")
conn.exec!("#{rvmsudo_path} #{rvm_path} gemset create #{project_name}")
end
I get my gemset created under, all good:
/usr/local/rvm/gems/ruby-1.9.3-p286#my_project_name
Here is my Capistrano setting for rvm/ruby:
set :rvm_type, :system
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
When I'm trying to deploy using capistrano, I get:
/usr/local/rvm/environments/ruby-1.9.3-p286#my_project_name: Permission denied
The environment file 'ruby-1.9.3-p286#my_project_name' is actually missing in that folder. I need to log into the server and navigate to my project so the .rvmrc file to trigger the creation of the environment (rvm --create my_project_name). I would like to avoid this last step. Do you know how to trigger the creation of this environment? (I though it would create it when I have created the Gemset)
RVM has support for installing itself via capistrano, to install rubies and to create gemsets, you should make use of it:
https://rvm.io/integration/capistrano/#install_rvm
https://rvm.io/integration/capistrano/#install_ruby
Basically the RVM environment need to be loaded to be able to script it remotely.
https://rvm.io/workflow/scripting/
You can run the following command with net-ssh to do so:
conn.exec!("source "/usr/local/rvm/scripts/rvm; rvm --create ruby-1.9.3-p286#my_project_name")
It will create your environment and associated gemset.

Rake tasks from cron - uninitialized constant YAML::ENGINE

I am getting uninitialized constant YAML::ENGINE when running a rake task from cron since I upgraded my server to ruby 1.9.2. I had the same error with the app but putting ...
require 'yaml'
YAML::ENGINE.yamler= 'syck'
in the boot.rb file fixed it. If I run the task directly from the command line on my Ubuntu server it works fine, the server uses RVM.
However running a task from cron doesn't seem to pickup this fix, I have tried this ...
task :twitter, :needs => :environment do
require 'yaml'
YAML::ENGINE.yamler= 'syck'
#tweets = Property.updatetwitter
end
to no avail.
Are you sure you're running it under Ruby 1.9.2? Because while YAML::ENGINE exists in 1.9.2, it's not in 1.8.7. Check your Ruby version.
UPDATE
How to tell which Ruby version program is using from within the program:
puts `ruby -v`
Lame way how to enforce cron task to run under certain Ruby version (if server uses RVM):
rvm use 1.8.7; ...

Register a script to use with the rails command, like `rails mycommand`

The rails command-line command provides a couple of commands, like rails generate, rails console etc. Now I'd like write a gem which registers my own command for use with rails mycommand.
Is this possible?
If so, any guides on how to do that?
NB: This is for rails 3+
regards, apeiros
Haven't done it, but here are some leads. In your Rails app, the script directory holds a file called 'rails' that has this line
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
It then requires this Rails file: https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb

Rails. "unitialized Contsant Object::Contact"

I'm working through the Ruby on Rails Bible using Windows 7 and Rails 3 + mysql.
I created a database and a table in mysql directly as per instructions.
Then I created a model called Contact
Next in irb I entered:
my_contact=Contact.new and then I get the error:
"unitialized constant Object::Contact"
I think perhaps I have to precede the code with a require statement or perhaps I need to install a gem? Except I haven't a clue beyond that at this stage as I'm a newbie...
Instead of running irb manually, run rails console, this should load all dependencies for your app. Also make sure you have run rake db:migrate before starting the console.