【问题标题】:Why won't rails recognize my model?为什么 Rails 无法识别我的模型?
【发布时间】:2013-06-13 05:18:07
【问题描述】:

很想听听有人对此的看法。我的 rails 应用程序已连接到开发数据库,​​我的所有模型都可以正常工作,除了一个。就应用程序而言,“消息”模型不存在,即使表已创建并且可以在 schema.rb 文件中看到。有没有人遇到过这个?我已经检查了我所有模型文件名的拼写,所以除非我完全没有注意到,否则我认为不是这样。模型如下所示:

class Message < ActiveRecord::Base
  has_paper_trail
  attr_accessible :content, :prescription, :type, :to, :sent_at, :created_at, :updated_at
  has_many :adherences
  has_many :patients, :through => :adherences
  validates :content, presence: true, length: { maximum: 160 }
  accepts_nested_attributes_for :patients
end

Rails 控制台输出什么也不显示:

1.9.3-p194 :001 > Message.all
NoMethodError: undefined method `all' for Message:Class
    from (irb):1
from /usr/local/rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

1.9.3-p194 :004 > Message.inspect
"Message"

虽然数据库创建了 Messages 表。 schema.rb(缩写):

ActiveRecord::Schema.define(:version => 20130613020005) do

  create_table "messages", :force => true do |t|
    t.string   "content"
    t.string   "type"
    t.string   "prescription"
    t.string   "to"
    t.datetime "sent_at"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

我唯一能想到的是“消息”模型存在于我正在使用的其中一个 gem 或其依赖项中的某个位置。但它之前工作正常,然后突然之间,不是。

以下是最近安装的 gem:

group :development, :test do
  gem 'sms-spec'
  gem 'rb-fsevent', '0.9.1', :require => false
  gem 'growl', '1.0.3'
  gem 'better_errors'
  gem 'meta_request'
  gem 'binding_of_caller'
  gem 'guard-spork', '1.2.0'
  gem 'childprocess', '0.3.6'
  gem 'spork', '0.9.2'
  gem 'shoulda-matchers'
end

如果有人遇到过这种情况或类似情况,我很想知道您的情况。关联可能还有其他问题等等,但据我所知,这似乎不是问题。

【问题讨论】:

  • Message.classMessage.superclass 可能会帮助您了解它是否由 Rails 的另一部分定义
  • Message.is_a?(ActiveRecord::Base)Message.class.methods.sort 也可能有帮助
  • 您在创建迁移后是否运行了 rake task migrate? (我知道不太可能,但如果)

标签: ruby-on-rails ruby-on-rails-3.2 gem rails-activerecord


【解决方案1】:

据我所知,growl gem 中有一个相互冲突的“消息”方法。我需要找到解决方法或删除该宝石。

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2020-05-28
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多