Don Werve
Don Werve

Reputation: 5120

Can't set UUID primary key on Rails 3.1

Old code, works fine in Rails 3.0 on JRuby 1.6.4 with PostgreSQL 9.0:

class User < ActiveRecord::Base
    before_validation(:on => :create) do
        self.id = generate_random_uuid
    end

    # Mode code, including a definition for generate_random_uuid that works.
end 

Rails 3.1 silently fails to set the primary key.

Did a bit of digging, and came up with update_column, but this refuses to do anything until the record has been persisted. Unfortunately, not too many people are using UUID primary keys in Rails-land, so the Googles aren't that useful.

Anybody else running a similar setup, that's figured out the right callback magic to get 3.1 to play ball?

Upvotes: 1

Views: 759

Answers (1)

Atastor
Atastor

Reputation: 741

Well..only just today noticed, that an app involving several legacy databases had stopped working after upgrading from 3.0 to 3.1 just for this reason, i.e. the primary key doesn't default to id anymore for legacy databases (and the like?).

My so far working solution was to explicitly give a

set_primary_key :id

in each of the models in question.

Regards Michael

Upvotes: 2

Related Questions