I create uids using
create_table :users, { id: false } do |t| t.uuid :uid, default: 'uuid_generate_v4()' ... other columns
and setting self.primary_key = :uid in models.
All in all, this works great with ActiveRecord, and I'm writing has_many and belongs_to . However, when crossing the connection table (i.e. has_many ... through: I need to write my own SQL to get the records.
I realized that in general I can do this by writing my own SQL, i.e. SELECT * FROM main_table JOIN join_table ON main_table.uid = cast(join_table.uid AS uuid) WHERE condition=true)
I recently realized that ActiveRecord create , destroy , save and update do not work on the merge model.
I fixed four methods to make them work, but this is too complex a sequence for my taste and probably not optimal. Here are my patches:
def save(*args)
Sometimes save issues a ROLLBACK for the first time without explanation. Then it works a second time. In other situations (I'm not sure why, perhaps when updating), the first time it succeeds, but if it is called a second time, a TypeError occurs. See here for another question about this error, in which there are no answers to the question of how to save the connection when using uid instead of id. Here are my other (working) patches.
def create(*args) attrs = args[0] raise( ArgumentError, "invalid args to bucket list create" ) unless attrs.is_a?(Hash) bucket_list_photo = self.class.new( attrs ) bucket_list_photo.save bucket_list_photo = BucketListPhoto.find_by( bucket_list_uid: bucket_list_photo.bucket_list_uid, photo_uid: bucket_list_photo.photo_uid ) return bucket_list_photo end def update(*args)
I even guaranteed that self.primary_key = :uid in all my models.
I also tried replacing uid with id everywhere and checked that all specifications passed (although I remained in the patch). However, it still failed when I deleted the patch (i.e. renamed the uid columns to id did not fix it).
EDIT
In response to some comments, I tried using activeuuid gem (where I was stuck with an error ) and decided to completely switch to ids. This is mainly for simplicity, since I have the pressure to launch this application as soon as possible.
However, even with this fix, I need to schedule save , create and update . Actually, the delete patch no longer works, and I had to delete it (relying on the original). I would definitely like to avoid the need to make these patches, and I keep the generosity open for this reason.