I have a system where I want a global identifier for objects in rails. The global identification is a number (or alpha-numeric) that is shared across a bunch of objects. When you save an object you will insert a global identificaiton value that is based upon an object and an object_id.
So for example:
location id=3
id=15
arc_type='location'
arc_id=3
arc_value=loc-3
So the question is can I do a has_one using a composite foreign key of object_type and object_id. Or would I need to a foreign key like the object value, a shortened name and the id of the original object.
Or perhaps use a different scenario such as an md5 hash as foreign key.
Would this be a candidate for doing a polymorphic association? It seems like that would be more appropriate for the has_many rather than has_one (such as pictuers or comments). Has anyone ever done a has_one with a polymorphic assoication?
thx
edit – looks like the object_id name is a bad idea so i’ve substituted arc_ which is initials of project
I’m not sure about the composite foreign key in Rails since the
has_oneassociation only seems to have:primary_keyand:foreign_keyoptions. See more here.I have accomplished something very similar to this recently though. At a high level, I added a
uuidfield to all the models that I want to uniquely identify along with a shared bit of code that generates the UUID on save. Now that each object has a unique identifier, I can use Rail’s:primary_keyand:foreign_keyto build associations as I please. Here’s the breakdown:I used the uuidtools gem to generate a universally unique id.
Then I created some helper methods in app/models to ensure that a UUID is generated and set whenever an ActiveRecord model is created or saved.
Finally, I make sure that my models of interest have a
uuidfield of type string and set up my associations.I hope this helps.