Linux‎ > ‎

Ruby & Rails

Clear Association Cache

posted May 12, 2017, 7:39 PM by Chris Martin   [ updated May 12, 2017, 7:40 PM ]



posted Apr 9, 2016, 5:33 AM by Chris Martin

Time format and zone

To display time in the local time zone

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

Useful Stuff

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

#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 May 2, 2014, 6:55 PM 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 Dec 29, 2013, 7:48 PM by Chris Martin   [ updated Jun 18, 2021, 7:33 AM ]

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 libreadline8 libreadline-dev curl git zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libncurses-dev automake libtool bison subversion pkg-config git-flow

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 1.9.3p545 (2014-02-24 revision 45159) [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 Dec 29, 2013, 3:47 PM by Chris Martin   [ updated Jan 1, 2014, 5:34 AM ]

JetBarins / RubyMine -
Aptana / Studio3 -

Aptana / Studio3 / RadRails -

1-6 of 6