以下是设置:
class Project < ActiveRecord::Base
has_many :project_subject_areas, class_name: "Project::SubjectArea"
has_many :subject_areas, through: :project_subject_areas
end
class SubjectArea < ActiveRecord::Base
has_many :project_subject_areas, class_name: "Project::SubjectArea"
has_many :projects, through: :project_subject_areas
end
class Project::SubjectArea < ActiveRecord::Base
belongs_to :project
belongs_to :subject_area, class_name: '::SubjectArea'
end
在Rails中运行:
[ SubjectArea.to_s, Project::SubjectArea.to_s ].inspect
给出:
["SubjectArea", "SubjectArea"]
但运行此服务器(服务器重启后):
[ Project::SubjectArea.to_s, SubjectArea.to_s ].inspect
给出:
["Project::SubjectArea", "SubjectArea"]
文件结构是:
models/project.rb
models/subject_area.rb
models/project/subject_area.rb
我可以手动使用require project/subject_area
绕过这个,但是我对这个命名空间做错了吗?
还有其他方法来命名我的模型吗?
当Rails生成名称空间rails generate Project::X
时,我将Project作为模块,但我需要它作为ActiveRecord类。我还有一些属于Project的其他模型,我真的希望它们在命名空间和模型/项目/目录中。