我有一个Ruby on Rails项目,已经有一段时间没有被开发过了,我正在尝试将它从Rails2.0升级到3.1。
当我尝试实例化其中一个模型时,我得到了一个错误。似乎其中一个模型也被定义为模块,这阻止了我实例化它。
dgs@dgs-desktop ~/code/spelling $ rails c
Loading development environment (Rails 3.1.1)
ree-1.8.7-head :001 > Spelling.first
NoMethodError: undefined method `first' for Spelling:Module
from (irb):1
ree-1.8.7-head :002 > exit拼写类非常基础:
class Spelling < ActiveRecord::Base
belongs_to :word, :class_name => 'Word', :foreign_key => 'word_id'
end我找不到在应用程序中定义这个模块的位置(它很小):
dgs@dgs-desktop ~/code/spelling $ cd app
dgs@dgs-desktop ~/code/spelling/app $ grep Spelling * -R
models/spelling.rb:class Spelling < ActiveRecord::Base
models/word.rb: has_many :spellings, :class_name => 'Spelling', :foreign_key => 'word_id'
models/spelling_user.rb:class SpellingUser < ActiveRecord::Base
views/layouts/application.html.erb: <title> School Spelling Tests</title>
dgs@dgs-desktop ~/code/spelling/app $ find ./ -name "spelling*"
./views/spellings
./views/admin/spellings
./models/spelling.rb
./models/spelling_user.rb有人知道这可能是什么原因吗?或者我怎么才能追踪到这个模块是在哪里定义的?
发布于 2012-01-26 01:39:44
所以。。。。在完全找错地方找了很久之后,我找到了(显而易见的)答案。
dgs@dgs-desktop /tmp/spelling $ cat config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
module Spelling
class Application < Rails::Application
...
end
end在从Rails2.x升级到Rails3.1的过程中,应用程序名变成了一个模块。因为我有一个与应用程序同名的模型,所以这个模型失败了。
(在我的greps中,我曾在application.rb中看到过这一行,但忽略了它)
应用程序的其余部分工作得很好,只有当我达到依赖于这个模型的部分时,它才会失败。当我将整个应用程序一位一位地复制到一个临时应用程序(名为spelling_new)中时,一切都正常工作,所以我认为这一定是原始应用程序中的一些缺点,并将其重命名为spelling_new -> spelling。在这一点上,一切都再次爆炸,罪魁祸首变得清晰起来。
发布于 2012-01-25 05:15:01
试试这个:
Spelling.ancestors这将给你所有的父类和模块,这将给你一个线索,它在哪里。如果这不起作用,请查看load_path变量:
y $LOAD_PATH它将为您提供路径列表,但您必须找到与您的代码冲突的路径。它相当多,但不应该很难,因为大多数gem都有良好的名称空间,所以最有可能的是,它是一个自定义补丁。
https://stackoverflow.com/questions/8993696
复制相似问题