Avoiding Duplicates in a Ruby on Rails Factory Girl Factory with Fake
The simplest thing to do is probably to use FactoryGirl's sequence:

FactoryGirl.define do
  factory :company do
    sequence(:name) { |n| "#{Faker::Company.name} #{n}" }
  end
end


sequence basically gives you an auto-incrementing integer, so you can avoid uniqueness issues. Sure, it'll generate some slightly odd company names, but for testing that shouldn't matter much.

In general though, I don't feel too great a need for something like Faker. Something like this should work just as well:

FactoryGirl.define do
  factory :company do
    sequence(:name) { |n| "Acme Inc. #{n}" }
  end
end


If it's only your specs looking at the data, it doesn't really matter if the company name's "Acme", "foobar 42", or "ACC8B1B7-7EC0-4F05-AB2A-B487C134F6BF".

Besides, your current solution is:


non-deterministic, so it may never exit the while loop if it just happens to keep picking the same couple names again and again,
but even if we ignore that, the act of adding more tests that use company records, will mean more name collisions and longer time spent in the loop. I don't know how Faker works, but (unlikely but technically possible) you might even end up exhausting every name Faker can come up with, and end up looping forever.