在我的 Rails 应用程序中,我有这样的模型
class Account < ActiveRecord::Base
class Account::Bonus < Account
class Account::Virtual < Account
...more
像这样
class DiscountSystem < ActiveRecord::Base
class DiscountSystem::MyFirst < DiscountSystem
class DiscountSystem::MySecond < DiscountSystem
Account and DiscountSystem - STI 模型
之后,我希望 DiscountSystem 有 Account,仅用于 DiscountSystem 的帐户,并将新模型命名为 Account::DiscountSystem
如果我理解正确,“Account”的“DiscountSystem”在命名空间“Account”中。
他们:
class Account::DiscountSystem < Account
belongs_to :discount_system,class_name: '::DiscountSystem',:foreign_key => :owner_id
end
和
class DiscountSystem < ActiveRecord::Base
has_one :account, :class_name => 'Account::DiscountSystem',:foreign_key => :owner_id
end
如果我在控制台中尝试
>> DiscountSystem::MyFirst.all
LoadError: Expected C:/.../app/models/account/discount_system.rb to define DiscountSystem
>> DiscountSystem.all
NoMethodError: undefined method `all' for DiscountSystem:Module
为什么?我可能是一个非常误解的命名空间概念。
当然,如果我将 Account::DiscountSystem 重命名为 Account::ForDiscountSystem,一切正常。为什么命名空间帐户“重写”根名称 DiscountSystem?