FBISurveillance
Joined 9 years ago
Karma
1,247
Submissions
291
Member since
Sep 25
Joined 9 years ago
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
include PrimaryKeyGenerator
end
# app/models/concerns/primary_key_generator.rb
module PrimaryKeyGenerator
extend ActiveSupport::Concern
included do
after_initialize :generate_id
end
def generate_id
return if self.class.attribute_types["id"].type != :uuid
return if id.present?
self.id ||= SecureRandom.uuid_v7
end
end
This also gives you the advantage of having your PK known before record persisted (maybe it's just me but I like my UUID PKs generated in app instead of db)I wish the TypeScript/React integration was easier. Say what you will but there's no way you can achieve interactivity and convenience of React (et al) UIs with Turbo/Hotwire in a meaningful time.
email: hnfriends31415 at gmail