【发布时间】:2011-10-25 09:59:25
【问题描述】:
我有一个使用 ActiveRecord 的 ruby 应用程序。我有以下型号
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
使用以下迁移
create_table :compositions do |t|
t.references :link, :polymorphic => true
end
create_table :chains do |t|
t.string :name, :null => false
end
当我创建一个具有 2 个关联组合的新链时,SQL 表“组合”包含:
标识 |链接ID |链接类型
1 | 1 | ExchangeManager::Resources::Chain
2 | 1 | ExchangeManager::Resources::Chain
我希望 ActiveRecord 只保存短类名,即“Chain”而不是“ExchangeManager::Resources::Chain”,而不是在 link_type 列中包含完整的命名空间。
为什么?因为我在另一个 Rails 应用程序中使用相同的数据库,并且我希望能够在没有命名空间的情况下操作该项目中的相同模型。
【问题讨论】:
-
如果你只把“Chain”放在数据库中,Rails 将如何在实例化时找到类?
-
Rails 应用程序中的模型不使用命名空间。只有 ruby 应用程序。
标签: ruby activerecord