fatal: cannot exec ‘/tmp/…/git-ssh.sh’: Permission denied – Capistrano, Dreamhost, permission denied for git-ssh.sh

Problem

When trying to use the new Capistrano 3.x to set up your rails project in a dreamhost account, you get the following error complaining that the git-ssh.sh script copied to your account by capistrano cannot be executed as the permission is denied:

fatal: cannot exec '/tmp/example.com/git-ssh.sh': Permission denied

Solution

It seems that Dreamhost, and quite possibly other hosting providers are not allowing executables from the /tmp directory, which is where Capistrano places the git-ssh.sh script. So in order to be able to execute the script you can change the directory where the script is copied in the first place and put it in your home directory. You can do that by adding the following to the config/deploy.rb file:

set :tmp_dir, "/home/dh_user_name/tmp"

Creating an alias that migrates both development and test databases in rails project

Problem

In your rails project very often, after creating a new migration, you have to run the migration in the development database and then in the test database.

Solution

A neat way to combine both these steps as one, taken from the book Rails 4 in Action, is to create an alias in your ~/.bashrc configuration file with the following, so that you only have to run migrate after each migration that would apply the migration in both development and test databases:

alias migrate='bin/rake db:migrate && bin/rake db:test:prepare'

build association for has_one in rails

Problem

You would like to use the build method for creating an association (details) that belongs to another assocation (user).
When the user has many details then you would have something like the following:

class User < ActiveRecord::Base
  has_many :details
end

then you could do:

User.details.build

but when you have:

class User < ActiveRecord::Base
  has_one :detail
end

would throw an error:

NoMethodError: undefined method `build' ...

Solution

In order to be able to use the build in a has_one relation you would need to use like:

User.build_detail

libv8 error in new rails installation

Problem

You are getting a libv8 installation error in a new rails 4.0 application in an older linux installation

An error occurred while installing libv8 (3.16.14.3), and Bundler cannot continue.
Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before bundling.

Solution

This is causes by the latest gem version of the therubyracer dependency on the libv8.
You can get over it by specifying version 0.11.4 for the gem as in:

gem 'therubyracer', '~> 0.11.4', platforms: :ruby

and then running bundle install again.

db:migrate or db:schema:load for Rails project with many migations

Problem

You have a rails application with many migrations build over time and you want to recreate the database from start. Should you be using the normal way of running the migrations (db:migrate) or the one that loads the actual schema to the database (db:schema:load).

Solution

According to the book Rails 4 in Action (MEAP v9 page 146) :

The bin/rake db:migrate task runs the migrations and then dumps the structure of the database to a file called db/schema.rb. This structure allows you to restore your database using the bin/rake db:schema:load task if you wish, which is better than running all the migrations on a large project again! NOTE

NOTE: Large projects can have hundreds of migrations, which may not run due to changes in the system over time. It’s best to just use the bin/rake db:schema:load.

Disabling SQL logging in rails development

Problem

You are working on a new feature that outputs a lot of SQL code in your log file so it’s difficult to see what else is happening, and you would like to disable the SQL logging in your development environment temporarily.

Solution

You can add the following to your config/application.rb file:

if Rails.env.development?
  ActiveRecord::Base.logger = Logger.new('/dev/null')
end

Thanks to the solution here

Interrupting a cucumber test

Problem

When you interrupt a cucumber test, usualy by presssing Ctrl+C twice, the database tables are not truncated, so you consequently could have a problem with duplicates records.

Solution

To make sure that the database is clear before running the test again run the following tasks to clear the database and clone it again:

rake db:test:purge
rake db:test:clone

Getting rails console on an amazon aws server when using rvm

Problem

You would like to get access to your rails application console on an Amazon ec2 instance, and you are also using rvm.

Solution

  • Login with ssh to your server as normal:
    ssh name@myserver.com -i amazon_key
  • Go to your application’s current folder:
    cd /my/project/directory/current/
  • Run the following replacing the environment with your specific environment (ie production,beta,staging etc):
    bundle exec rails c environment

Deleting old releases from rails app server using puppet

Problem

You would like to automatically delete old releases from your rails app server using a puppet crontab resource.

Solution

Add the following to your modules/crontab/manifests/init.pp file and modify some of the values to what you need:

 # use // to suppress warning: Warning: Unrecognised escape sequence '\;'
  cron { remove_old_releases:
    command   => "test $(find /var/www/your/path/to/ror/app/ -maxdepth 1 -type d | wc -l) -gt 10 && find /var/www/your/path/to/ror/app/ -maxdepth 1 -type d -mtime +14 -exec rm -rf '{}' \\;",
    user      => ubuntu,
    hour      => 0,
    minute    => 15
  }

You should need to change the following:

/var/www/your/path/to/ror/app/ to your application’s release path
-gt 10 this is the number of minimum old releases you want to keep
-mtime +14 releases older than 14 days are deleted

UPDATE

There is chance that when you do not calculate the frequency of the releases right the above script can potentially delete all of your releases, so you end up with no releases 🙁

A better solution to keep the -n number of releases (based on the solution from here:

find /var/www/your/path/to/ror/app/* -type d -printf '%T@ %p\n' | sort -nr | tail -n+6 | cut -f 2- -d " "  | xargs -i rm -rf {}

and the explanation of each step:

  • find /var/www/your/path/to/ror/app/* -maxdepth 0 -type d -printf ‘%T@ %p\n’ find all the directories (-maxdepth 0 -type d) in the search path (/var/www/your/path/to/ror/app/*) excluding the . and .. directories (/*) and add to them the time information (‘%T %p\n’)
  • sort -nr sort them in numeric and reverse order – newer first, oldest last
  • tail -n+6 keep only the first 5 directories, starting from line 6 (tail -n+6)
  • cut -f 2- -d ” “ Remove the first field (ie the date information) keeping the file from the second field using the space as the delimiter (-d ” “)
  • xargs -i rm -rf {} Pass the rm -rf command to delete the directory for each line produced in the previous steps

So the puppet script should be as follows (including the escape characters \ for the % and the additional \):

 # use // to suppress warning: Warning: Unrecognised escape sequence '\;'
  cron { remove_old_releases:
    command   => "find /var/www/your/path/to/ror/app/* -maxdepth 0 -type d -printf '\\%T@ \\%p\\n' | sort -nr | tail -n+6 | cut -f 2- -d ' ' | xargs -i rm -rf '{}' \\;",
    user      => ubuntu,
    hour      => 0,
    minute    => 15
  }