Linux‎ > ‎

Ruby & Rails


posted 9 Apr 2016, 05:33 by Chris Martin

Time format and zone

To display time in the local time zone

#== TimeZone
config.prepend_before_action -> { = "Sydney" }

Useful Stuff

posted 5 Mar 2015, 23:03 by Chris Martin   [ updated 5 Mar 2015, 23:08 ]

#include v #eager_load v #preload


eager_load generates one long query
preload generates two queries
include automatically decides to use either eager_load or preload

RoR (Ruby on Rails) primer

posted 2 May 2014, 18:55 by Chris Martin

To create a new rails project and populate with the required directories etc use

rail new <project name>

Install Gems required for a project that are specified in the Gemfile.
It is good practice to run this command when eve you go a git checkout or pull

bundle install

Update the database scheme
It is good practice to run this command when eve you go a git checkout or pull

rake db migrate

Install Ruby using RVM

posted 29 Dec 2013, 19:48 by Chris Martin   [ updated 27 Apr 2014, 21:20 ]

Don't install the ruby packages supplied with your linux distro.  they are usually out of date
Instead, install via the RVM (Ruby Version manager). This will also allow you to install different versions and configurations for different projects.   The beautiful part of this is that it installs RVM and Ruby to our home directory, providing a sandboxed environment just for us.  

First update the app repository and install some prerequisites.
$ sudo apt-get update
$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config

Now install RVM
$ sudo apt-get install curl
$ curl -L | bash -s stable --auto-dotfiles # This installs RVM

reload our shell environment
$ . ~/.bash_profile

The next problem you will encounter is that ~/.bash_profile is only automatically sourced if you have logged into the shell.  If you are opening a terminal window you are not going to have the functions included automatically.  If you look in  ~/.bash_profile you will see that RVM added the following:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

You will now need to remove this from ~.bash_profile and add it to ~/.bashrc

Install the packages we need:
$ rvm requirements

Install Ruby
$ rvm install 1.9.3

Now tell rvm that we want to use this version 
$ rvm use 1.9.3 --default

Check the version
$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]

Users can, and should, use a gemset when possible so that they don't pollute their 'default' which is what is selected when a gemset is not specified in either a project's .rvmrc, or at the command-line. Each installed Ruby has a '@global' gemset. This is used to share gems with other gemsets created under that specific Ruby, and with the 'default' gemset. This can be selected by running 'rvm gemset use global' and then installing the gems you wish to share to other gemsets including 'default'. You can, of course simply install in each gemset but this will cause needless duplication and use up more disk-space and bandwidth.

Now that we have installed RVM and a version of Ruby, we can install Rails. Because RVM is installed to our home directory, we don't need to use sudo to install things.  This will install the rails gem and the multitude of gems that it and its dependencies depend on, including Bundler.
$ gem install rails -v 3.2.16

Install the Postgresql gem:
first make sure that postgresql is installed
$ sudo apt-get install postgresql

install the gem:  note the dependency on libpq-dev (for Ubuntu)
$ sudo apt-get install libpq-dev
$ gem install pg

Get a copy of the database and install it in to your own local database (see the posting on postgress backup and restore)

Next you can checkout the source using git (the separate post on setting up git).

To get all the gems loaded in your project change to the root directory of the application and run
bundle install

To run the application in a web server use
rails server


posted 29 Dec 2013, 15:47 by Chris Martin   [ updated 1 Jan 2014, 05:34 ]

JetBarins / RubyMine -
Aptana / Studio3 -

Aptana / Studio3 / RadRails -

1-5 of 5