Rails控制台:无法自动加载常量

dmt*_*989 22 associations autoload rails-console ruby-on-rails-4

我有一个Customer_ratings模型,允许用户互相留下反馈.Web应用程序正常运行,并收集,存储和显示反馈.

我想通过rails控制台进入并删除一些反馈,但是当我输入时Customer_rating.all,我收到以下错误:

LoadError: Unable to autoload constant Customer_rating, expected /Users/myapps/app/models/customer_rating.rb to define it
Run Code Online (Sandbox Code Playgroud)

同样,如果我输入Customer_rating [0],我会得到:

RuntimeError: Circular dependency detected while autoloading constant Customer_rating
Run Code Online (Sandbox Code Playgroud)

通过我的控制台访问其他表时我没有这个问题.

可能导致此问题的原因,以及为什么此错误不会禁止Customer_ratings通过Web应用程序正常工作?

Kir*_*rat 41

这似乎是一个混乱的命名约定的情况.

根据Rails命名约定,文件名应该在snake_case类名CamelCase.在您的方案中,文件名应该是customer_rating.rb,类名应该是CustomerRating.

进行这些更改后,使用CustomerRating.all(作为更新的类名称CustomerRating)来获取所有客户评级.不要用 Customer_rating.all.


Rya*_*ley 7

我还想添加一个我发现的问题的场景,以供将来参考.

我正在运行Rails 4.0并且我遇到了同样的问题,但发生的事情是我在student.rb中有一个名为Student的模型,该模型包含在名为Student的文件夹中.起初我没有意识到它,但文件夹名称是问题.将文件夹名称更改为模型名称以外的其他名称可解决此问题.