Moncef Belyamani

Coding, productivity, and Mac automation tutorials for beginners and beyond.

Rails: Prevent the Destruction of Child Object When Parent Requires Its Presence

Let’s say we’re building a Rails app where the Location model has_one Address, has_one MailAddress, and requires the presence of at least one of them. To ensure that a Location cannot be created without any type of address, we can add the following lines to the Location model:

1
2
3
4
5
6
7
8
9
10
11
validates(
  :address,
  presence: { message: 'A location must have at least one address type.' },
  unless: proc { |loc| loc.mail_address.present? }
)

validates(
  :mail_address,
  presence: { message: 'A location must have at least one address type.' },
  unless: proc { |loc| loc.address.present? }
)

How to Upgrade PostgreSQL With Homebrew

TL;DR If you’ve just upgraded Postgres with Homebrew and Postgres won’t start, as long as you don’t care about any data stored locally, remove all versions of Postgres (brew remove --force postgresql), delete the /usr/local/var/postgres/ folder, then reinstall Postgres.

I’m currently doing some Rails consulting for a company that uses Postgres in their app. After cloning the app to my computer, I wanted to make sure I had the latest version of Postgres.

RSpec & Capybara: How to Fix Undefined Method `feature’ for main:Object (NoMethodError)

While working on the Ohana API during the Code for America fellowship, I came across a strange situation. My feature specs didn’t include require "spec_helper" at the top of each file, yet I was able to run all of the specs like so:

1
$ rspec

Or just all of the feature specs:

1
$ rspec spec/features

And individual ones as well:

1
$ rspec spec/features/signin_spec.rb

Then when I cloned the repo to a new directory (to test the installation instructions in the README), I noticed that I could no longer run specific tests. If I tried to run rspec spec/features/signin_spec.rb for example, I would get this error:

1
2
3
4
5
6
7
8
/Users/monfresh/Desktop/ohana-api/spec/features/signin_spec.rb:1:in `<top (required)>': undefined method `feature' for main:Object (NoMethodError)
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load'
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `each'
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:22:in `run'
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
  from /Users/monfresh/.rvm/gems/ruby-2.0.0-p353@ohana-api/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'

The solution was to add require "spec_helper" to the top of each feature spec, but what I still haven’t been able to figure out, is why I was able to get by for months without that require statement. If you know why, please send me a Tweet to enlighten me!

Why I’m Coding for America

May 23, 2012. I’m attending TechCrunch Disrupt NYC with several AOL colleagues to celebrate the release of the TechCrunch iPad app. We’d been given the opportunity to attend the last day of the conference in recognition of our hard work.

Todd Park (the CTO of the United States) and Steven VanRoekel (the US CIO) happened to be speakers that day, and their passionate and spirited talk on innovation in the government blew me away. I started following Todd Park on Twitter, and that same day, he mentioned Code for America in a tweet. I had not heard of them before, and the name sounded intriguing, so I proceeded to follow them as well. Shortly thereafter, I found out about the open application for their Fellowship program.

How to Install PostgreSQL on a Mac With Homebrew and Lunchy

This is a quick guide for installing PostgreSQL (Postgres for short) on a Mac with Homebrew, and starting and stopping it with Lunchy. If you’ve just forked a Ruby on Rails project that requires Postgres, this tutorial will get you up and running in no time.

If you’re working on a brand new Rails app and need to create a new Postgres user and database for your app, I recommend Will Jessop’s tutorial or Peter Cooper’s screencast.

List the Largest Folders on Your Mac in One Keystroke With Keyboard Maestro or Quicksilver

In preparation for backing up my large Dance Music library to Dropbox (in order to sync it between my iMac and MacBook Air), I wanted to start by deleting all the songs I won’t miss. Finding individual files over a certain size is easy via the Finder, but I wanted to find out which folders (i.e. albums) were over 500MB so I could tackle them first.

There is no Folder size option in Finder’s search (afaik), so I did some Unix command research and found the wonderful du (short for disk usage). By default, du lists the folder sizes as multiples of 512 bytes, but you can easily change that to 1024 bytes with the -k flag. So, if you wanted to see the size of all your Dropbox folders, you would run the following command in Terminal: