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

  • Home
  • SEARCH
  • 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 6723157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:32:08+00:00 2026-05-26T09:32:08+00:00

I have a ruby app that uses ActiveRecord. I have the following models module

  • 0

I have a ruby app that uses ActiveRecord. I have the following models

module ExchangeManager
  module Resources
    class Composition < ActiveRecord::Base
      belongs_to :chain
      belongs_to :link, :polymorphic => true, :primary_key => :id
    end

    class Chain < ActiveRecord::Base
      has_many :compositions
    end
  end
end

with the following migration

create_table :compositions do |t|
  t.references :link, :polymorphic => true
end

create_table :chains do |t|
  t.string :name, :null => false
end

When I create a new chain with 2 associated compositions, the SQL table ‘compositions’ contains :

id | link_id | link_type

1 | 1 | ExchangeManager::Resources::Chain

2 | 1 | ExchangeManager::Resources::Chain

Instead of having the full namespace in the link_type column, I would like ActiveRecord to save only the short Class Name, ie ‘Chain’ instead of ‘ExchangeManager::Resources::Chain’.

Why ? Because I am using the same DB in an another Rails app, and I would like to be able to manipulates the same models in that project without namespaces.

  • 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-26T09:32:08+00:00Added an answer on May 26, 2026 at 9:32 am

    That would require, I presume, some patching of the polymorphic loading mechanism. And I would advise against it.

    A better solution would be to have your own column which stores the type without the namespace.

    class FirstModel
      before_save :store_link_type
      def store_link_type
        self.link_short_type = self.link && self.link.class.name.split(':').last # or whatever
        return true
      end
    end
    
    class SecondModel
      belongs_to :link, :polymorphic => true, :foreign_type => 'link_short_type'
    end
    

    If you want to update the link from the second app, too, you’ll have to do the reverse operation of prepending the namespace as well.

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

Sidebar

Related Questions

I have a ruby on rails app that uses Heroku. I have the need
I have a Ruby on Rails app that I've recently deployed to a remote
I have VPS with 1GB of RAM. One ruby-app that runs on thin server
I have a Ruby script in my Rails app that I use to load
I have a Ruby on Rails app that needs process many background jobs simultaneously:
So I have a script in ruby that obviously uses variables. Part of the
I have a basic Ruby app that I am building with Sinatra, Datamapper and
I have a ruby on rails app that has a signup page. Different pages
I have a standard Rails app that uses a database, sqlite3 and the webrick
I have a Ruby on Rails app that has a scheduled job I execute

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.