原始代码如下所示:
# app/models/sso_configuration.rb
class SsoConfiguration < ActiveRecord::Base
end
# db/schema.rb
create_table "sso_configurations", force: true do |t|
...
end
我必须重命名模型并添加名称空间,以便拥有Sso::SamlConfiguration
。我更改了模型和数据库表。
# db/migrate20160225144615_rename_sso_configurations_to_sso_saml_configurations.rb
class RenameSsoConfigurationsToSsoSamlConfigurations < ActiveRecord::Migration
def change
rename_table :sso_configurations, :sso_saml_configurations
end
end
# db/schema.rb
create_table "sso_saml_configurations", force: true do |t|
...
end
# app/models/sso/saml_configuration.rb
module Sso
class SamlConfiguration < ActiveRecord::Base
end
end
当我打开Rails控制台时,会发生以下情况。
> Sso::SamlConfiguration
=> Sso::SamlConfiguration(Table doesn't exist)
> Sso::SamlConfiguration.new
=> PG::UndefinedTable: ERROR: relation "saml_configurations" does not exist
我最初的想法是,按照惯例,命名空间模型应该使用蛇形名称作为表名称,这样就Foo::Bar
应该具有一个对应的foo_bars
表。我的设置中缺少什么吗?
我想出了解决方案,方法是复制让我为我生成命名空间模型的rails会做什么
rails g model sso/test
invoke active_record
create db/migrate/20160226074853_create_sso_tests.rb
create app/models/sso/test.rb
create app/models/sso.rb
invoke rspec
create spec/models/sso/test_spec.rb
invoke factory_girl
create spec/factories/sso_tests.rb
我检查了这些新文件中的所有路径和名称约定,唯一遗漏的是文件app/models/sso.rb
。
创建以下内容解决了我的问题:
# app/models/sso.rb
module Sso
def self.table_name_prefix
'sso_'
end
end
然后
rails d model sso/test
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句