Dealing with unique indexes in FactoryGirl

Thu, Apr 6, 2017

I’m working on an app that has multiple currencies defined as database models which look like this:

create_table :currencies do |t|
  t.string :code, null: false, unique: true

The problem is, these match up to currencies in Because these currencies need to link up, I can’t just use a sequence in FactoryGirl.

I could register a new currency every time in money, but some of my production code actually relies upon currencies being certain values.

A way around this would be to cache currencies and reused them kind of like:

def gbp
  @gbp ||= create(:currency, :gbp)

This is kind of ugly, and would need to be reset for each test run because our database gets cleaned after every test.

It turns out there’s a better way with initialize_with in FactoryGirl:

FactoryGirl.define do
  factory :currency do
    initialize_with do
        code: code

This ensures that when you create(:currency) it will only be created once (per test run) for each currency code.