首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ruby 1.8.7中查找模块的源代码位置

在Ruby 1.8.7中查找模块的源代码位置
EN

Stack Overflow用户
提问于 2012-01-25 04:31:59
回答 2查看 2.3K关注 0票数 1

我有一个Ruby on Rails项目,已经有一段时间没有被开发过了,我正在尝试将它从Rails2.0升级到3.1。

当我尝试实例化其中一个模型时,我得到了一个错误。似乎其中一个模型也被定义为模块,这阻止了我实例化它。

代码语言:javascript
复制
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

拼写类非常基础:

代码语言:javascript
复制
class Spelling < ActiveRecord::Base
  belongs_to :word, :class_name => 'Word', :foreign_key => 'word_id'
end

我找不到在应用程序中定义这个模块的位置(它很小):

代码语言:javascript
复制
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

有人知道这可能是什么原因吗?或者我怎么才能追踪到这个模块是在哪里定义的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-26 01:39:44

所以。。。。在完全找错地方找了很久之后,我找到了(显而易见的)答案。

代码语言:javascript
复制
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。在这一点上,一切都再次爆炸,罪魁祸首变得清晰起来。

票数 -1
EN

Stack Overflow用户

发布于 2012-01-25 05:15:01

试试这个:

代码语言:javascript
复制
Spelling.ancestors

这将给你所有的父类和模块,这将给你一个线索,它在哪里。如果这不起作用,请查看load_path变量:

代码语言:javascript
复制
y $LOAD_PATH

它将为您提供路径列表,但您必须找到与您的代码冲突的路径。它相当多,但不应该很难,因为大多数gem都有良好的名称空间,所以最有可能的是,它是一个自定义补丁。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8993696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档