[rspec-users] Does RSpec work nicely with UUID primary keys?

Steve Eley steve at deepsalt.com
Sat Jul 5 11:10:31 EDT 2008

On Sat, Jul 5, 2008 at 10:03 AM, John Knox <lists at ruby-forum.com> wrote:
> But the auto-generated specifications created by "script/generate
> rspec_scaffold...", etc, assume that one is using a sequential integer
> primary key.

That's just the fixtures in spec/fixtures.  They're using the old
fixture style, where all the IDs are hardcoded.  You can modify the
fixtures easily enough simply by removing the ID field from each of
them; if you're using Rails 2.0 or higher, read up on the "foxy
fixtures" behavior where it handles ID generation and associations in
a much smarter way.

Better yet, for more flexibility, get rid of the fixtures entirely and
create your test data using some sort of factory method.  If you're
doing something fundamentally different with your data like UUID
primary keys, you might _have_ to do this.  The Peepcode screencasts
about RSpec show easy examples of how to define create_foo() methods
in your specs.  Or you could use one of the various fixture
replacement plugins/gems:

Fixture Replacement 2:
(This is the one I use.  I love it.)

Factory Girl:


If you use any of these, you will of course end up having to change
the autogenerated specs in spec/models.  But for models at least, the
RSpec generator puts virtually nothing in, so that only means changing
one or two lines of code each time.  I don't consider that much of a

Have Fun,
Steve Eley
Deep Salt Team

More information about the rspec-users mailing list