Dealing with unique indexes in FactoryGirl

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
end

The problem is, these match up to currencies in https://github.com/RubyMoney/money. 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)
end

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
      Currency.where(
        code: code
      ).first_or_initialize
    end
  end
end

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

[General]

Name=Magic Keyboard
Class=0x000540
SupportedTechnologies=BR/EDR;
Trusted=true
Blocked=false
Services=00001124-0000-1000-8000-00805f9b34fb;00001200-0000-1000-8000-00805f9b34fb;

[DeviceID]
Source=1
Vendor=76
Product=615
Version=102

[LinkKey]
Key=71AF26E400FD96740AE12CA2A2344A55
Type=5

PINLength=0
~

https://superuser.com/questions/422435/how-to-use-bluetooth-devices-under-two-different-operation-systems