未初始化的常量Model1 :: Model2 :: Model3

时间:2013-03-07 22:59:32

标签: ruby ruby-on-rails-3 ruby-on-rails-3.2

我正在<{1}}

uninitialized constant Project::Forum::Topic

我的代码如下,我正在从app/controllers/home_controller.rb:46:in `discussions' 转换为rails 2.3.x,我在rails 3.2.11设置中认为有问题。

任何想法我该如何解决?

模型

routes

home_controller.rb

class Project < ActiveRecord::Base
      # Relations under project model
      has_many :features, :dependent => :destroy
      has_many :forums, :class_name=>'Forum::Forum'
      has_many :topics, :class_name=>'Forum::Topic', :through=>:forums
class Forum::Forum < Feature
  # Relations under forum model
  has_many :topics, :class_name => 'Forum::Topic', :dependent => :destroy

class Feature < ActiveRecord::Base
  # Relations under feature model
  belongs_to :project

class Forum::Topic < ActiveRecord::Base
   # Relations under topic model
   belongs_to :forum, :foreign_key => :forum_id, :class_name => 'Forum::Forum', :include => :project

的routes.rb

def discussions
  @project ||= Project.find_by_name 'help'
  @forums = @project.forums
  @topics = @project.topics.recent # HERE I AM GETTING ERRORS
end

错误

scope :home, :controller => "home", :activity => 'read' do
 get :discussions, :path => '/forums', :service_type => 'public'
 get :forums, :action => "discussions"
end

2 个答案:

答案 0 :(得分:2)

我刚刚通过rails论坛回答,

在项目模型类中,更改以下方式

OLD:has_many :topics, :class_name=>'Forum::Topic', :through=>:forums

新:has_many :topics, :class_name=>'::Forum::Topic', :through=>:forums

它应该有用

答案 1 :(得分:0)

如果您自动加载该类(即,您没有require其源文件),原因可能是Ruby autoload的错误,自动加载常量嵌套3次或更多次。< / p>

目前我无法在互联网上找到此信息的来源;但是,我认为它应该用Ruby 2.0解决,所以你可以修复它从autoload中删除你需要的文件,在你需要的地方添加require 'project/forum/topic',或者升级到Ruby 2.0。