Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6615805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:30:26+00:00 2026-05-25T20:30:26+00:00

I have a system where I want a global identifier for objects in rails.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:30:27+00:00Added an answer on May 25, 2026 at 8:30 pm

    I’m not sure about the composite foreign key in Rails since the has_one association only seems to have :primary_key and :foreign_key options. See more here.

    I have accomplished something very similar to this recently though. At a high level, I added a uuid field 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_key and :foreign_key to build associations as I please. Here’s the breakdown:

    I used the uuidtools gem to generate a universally unique id.

    Gemfile.rb
    gem 'uuidtools'
    

    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.

    uuid_helper.rb
    
    module UUIDHelper
      def self.append_features(base)
        base.before_create { |model| base.set_uuid_if_empty(model) }
        base.before_save { |model| base.set_uuid_if_empty(model) }
    
        def base.set_uuid_if_empty(model)
          if column_names.include?('uuid') and model.uuid.blank?
            model.uuid = UUIDTools::UUID.random_create().to_s
          end
        end
      end
    end
    

    Finally, I make sure that my models of interest have a uuid field of type string and set up my associations.

    Thing.rb
    
    class Thing < ActiveRecord::Base
      include UUIDHelper
    
      belongs_to :user, :primary_key => 'uuid', :foreign_key => 'owner'
    end
    
    
    User.rb
    
    class User < ActiveRecord::Base
      include UUIDHelper
    
      has_many :thing, :primary_key => "uuid", :foreign_key => "owner"
    end
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have a web application that I want to run some system tests on,
I have a System.Windows.Forms.ComboBox on a form and I want to capture the event
I have a System.Windows.Forms.Button control, and I want to add a hover-text explanation of
I have an object of the type System.Drawing.Image and want to make every pixel
I want to ensure I have done all I can to configure a system's
I have a problem with the system function. I want to store the system
We have Xserver-less CentOS system for continuous integration. So no UI. I want to
I have a library, and I want to migrate it from an old system
i have a 5 stars rating system, on javascript!!! bau i want to update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.