2

这是设置:

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 的其他模型,我真的希望它们被命名空间并位于 models/project/ 目录中。

4

0 回答 0