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 =开发者_StackOverflow社区> :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?
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
精彩评论