find out the status of rails application migrations

Problem

You would like to know at any moment the status of your migrations, if they have been applied, rolled back etc.

Solution

There is a very usefull rake task that can give you the list of your migrations withouth having to look at your schema.rb file:

rake db:migrate:status

Status   Migration ID    Migration Name
--------------------------------------------------
   up     20140528102449  Create products
   up     20140613150126  Create carts
   up     20140613151119  Create line items
   up     20140617215027  Add quantity to line items
   up     20140617220031  Combine items in cart
   up     20140620131542  Create orders
   up     20140620131605  Add order to line item

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

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.

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

Creating a user token with Devise for other uses except log in into the system

Problem

You are using Devise in your rails application, and you want to have a token for authenticating an API call, but you don’t want to use the default authentication token so that.
The purpose of doing something like that is that you don’t want the application to have the same authentication rights as the devise authentication tokens.

Solution

Add the field to your user model (ie app_token).

On your user model create the following:

class User < ActiveRecord::Base

before_create :create_app_token
...
private
def create_app_token
  self.app_token = Devise.friendly_token
end

Adding a record in Rails through javascript

Problem

You would like to trigger a record insertion after a specific action has happened through javascript (ie drag – drop an item into a container), in a rails application.

 

Solution

You would need to get the object attributes from the html page somehow (ie user_id and user_name), and then have a function in your application js that calls the post function.

So if you have a drag and drop container for example then the code in the application.js should be like the following:

$("#name_of_container").droppable({
  drop: handleDropEvent
});

function handleDropEvent(event,ui) {
  var user_id = ui.draggable.attr("user_id");
  var user_name = ui.draggable.attr("user_name");
  $.post('/users', {user: {user_id: user_id, name: user_name}})
}

 

ERD diagrams in Ruby on Rails

Problem

You would like to have an ERD diagram of your database in your Ruby on Rails project.

Solution

You can install the Rails ERD from here, and install it by following the instructions.
For Ubuntu/debian systems should be:

sudo apt-get install graphviz

and then adding the gem to your Gemfile in the development section as:

group :development do
  gem "rails-erd", "~> 0.4.5"
end

You can then run bundle install to install the gem and rake erb to create the diagram pdf.