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.
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.
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.