Start delayed_job in the background

This starts delayed_job, hiding all the output and hiding the process in the background.

rake jobs:work &>/dev/null &

How to install the date_performance gem

sudo gem install zip
git clone git://github.com/rtomayko/date-performance.git
cd date-performance
rake package:build
cd dist
sudo gem install --no-ri --no-rdoc date-performance-0.4.7.gem

Clear a Solr index with acts_as_solr

ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "#{Location.solr_configuration[:type_field]}:#{ModelClass}"))
ActsAsSolr::Post.execute(Solr::Request::Commit.new)

Starting and stopping Solr

Solr listens to different ports for different environments.

Start Solr (and hide useless output)

rake solr:start PORT=8981 &>/dev/null
rake solr:start PORT=8982 &>/dev/null

Stop Solr

rake solr:stop PORT=8981
rake solr:stop PORT=8982

Run a single Cucumber feature

script/cucumber features/feature_name.feature

Or, if you don't care about speed, you can use rake:

rake features FEATURE=features/feature_name.feature

Install a gem without RI and RDdoc

To improve installation times of gems you can use the following approach:

gem install xyz --no-document

To permanently ignore ri and rdoc when installing gems, add this line to ~/.gemrc:

gem: --no-document

Be aware that eliding local documentation may disable documentation support in your IDE.

Calling class methods

To call a class method (or static method) from an instance method:

class.method()

To call a class method from a class method:

method()

Fixing "A copy of Klass has been removed from the module tree but is still active"

This can happen during development when classes without automatic reloading are pointing to classes with automatic reloading. E.g. some class in lib is calling Model.static_method.

Workaround A

Stop referencing autoloaded classes from static files. If you can't, see workaround B and C.

Workaround B

Make sure the offending file (the one referencing the autoloaded class) is autoloaded, too. You may do this:

# config/application.rb

  config.paths.add 'offending/file/parent/directory', eager_load: true

Workaroun...

Create an application with an older Rails version

sudo gem install rails --version="=1.2.3"
rails _1.2.3_ new-project-folder

Freeze a specific Rails version

sudo apt-get install unzip
rake rails:freeze:edge RELEASE=2.2.2

Show the current Git branch on your Bash prompt

Append this to your ~/.bashrc:

export PS1='\[\033[01;32m\]\h\[\033[01;34m\] \w\[\033[31m\]$(__git_ps1 "(%s)") \[\033[01;34m\]$\[\033[00m\] '

Reload the changes by saying

source ~/.bashrc

For some customized prompts that also show the current Git prompt, see the examples section at the bottom of our bash prompt customizing card.

Run a single test in Test::Unit

To run a single test file:

rake test:units TEST=test/unit/post_test.rb
rake test:functionals TEST=test/functional/posts_controller_test.rb
rake test:integration TEST=test/integration/admin_news_posts_test.rb

You may even run a single test method:

ruby -I test test/unit/post_test.rb -n "name of the test"
ruby -I test test/functional/posts_controller_test.rb -n test_name_of_the_test # underscored, prefixed with 'test_'

Or all tests matching a regular expression:

ruby -I test test/integration/admin_news_posts_test.r...

Typical .gitignore

log/*
tmp/*
storage/*
db/*.sqlite3
db/schema.rb
db/structure.sql
public/system
.project
.idea/
public/javascripts/all*
public/stylesheets/all*
public/stylesheets/*.css
config/database.yml
*~
*#*
.#*
.DS_Store
webrat-*.html
capybara-*.html
rerun.txt
coverage.data
coverage/*
dump_for_download.dump
.~lock.*
.*.swp
C:\\nppdf32Log\\debuglog.txt

Check out a remote branch in git

If you would like to checkout the branch groups, you can simply say this in recent versions of Git:

git fetch origin
git checkout groups

This will automatically track origin/groups from a local branch groups.

In older Git versions you need to say this:

git fetch origin
git checkout --track origin/groups

Git for Subversion users

This is for people recovering from Subversion.

Get an existing from the server for the first time

git clone git@example.com:repositoryname

See what's changed

git status

Check in locally

git commit -m "good description"

Push local commits to the server

git push

Get and merge updates from the server

git pull

Stage a file for the next local commit

git add file

Stage all files for the next local commit

git add .

Create a new local branch and check it out

git checkout -b branchname

...

Test if a checkbox is checked in jQuery

jqueryElement.is(':checked')

Use the ALT key in a VirtualBox Windows VM

In Ubuntu your ALT key is locked when you're working in VirtualBox. There are two workarounds for this:

  • Press the Windows key in addition to the ALT key
  • Run the attached registry file and restart Windows. Your left Windows key is now the ALT key.
  • If you'd like to map keys differently, here are further instructions

Order of the state_machine callback chain

  1. before transition
  2. before validation
  3. after validation
  4. before save
  5. after save
  6. after transition

Aborting the callback chain

See Cancel the ActiveRecord callback chain.

How to fix "Too many authentic authentication failures" with SSH and/or Capistrano

You are getting when connecting via SSH or deploying with Capistrano (which uses SSH):

Too many authentication failures for username

This is caused by having too many SSH keys added to your keyring or ssh-agent. Your ssh-agent will throw all keys against a server until one matches. Most servers will deny access after 5 attempts.

This issue might come and go as the order of the active SSH keys in your ssh-agent changes.

Quick fix

Have less keys. Up to 5 keys are fine when the SSHD you're connecting to is using the default ...

Why your all.js is empty on staging or production

When you include a non-existing Javascript file, you probably won't notice it during development. But with caching active (on production or staging) Rails will write an empty all.js file without complaining.

Slugs with FriendlyId

Gem to provide nice looking urls ("/blog/the-greatest-bug-i-never-fixed"). If you don't need anything too special (like i18n for the urls) it works as a drop-in-replacement. It basically overwrites #to_param to return the slug, and .find to search by the slug.

Make sure, everywhere you build paths, you use model_path(:id => model) instead of model_path(:id => model.id). You also need to adapt all code using something like .find_by_id. The regular .find is fine.

See the github README for installation instructions.

Don't forget ...

Copy a Paperclip attachment to another record

Just assign the existing attachment to another record:

new_photo = Photo.new
new_photo.image = old_photo.image

Paperclip will duplicate the file when saving.

To use this in forms, pimp your attachment container like this:

class Photo < ActiveRecord::Base
  has_attached_file :image
  
  attr_accessor :copy_of

  def image_url
    if copy_of
      copy_of.url
    else
      image.url
    end
  end
end

And in the controller do:

new_photo = Photo.new(:copy_of => old_photo)