【问题标题】:attr_accessible error on rails 4.1.8 upon performing rake db:migrate on heroku在执行 rake db:migrate on heroku 时,rails 4.1.8 上出现 attr_accessible 错误
【发布时间】:2015-08-04 16:59:19
【问题描述】:

我还在学习 Ruby(所以我完全是菜鸟), 现在我的应用程序在本地成功运行,但是当我尝试在 heroku 上打开应用程序时,我第一次执行 heroku run rake db:migrate 我偶然发现了一个问题.. 它告诉我:

Running `rake db:migrate` attached to terminal... up, run.2149
-- attr_accessible(:pName, :pQuantity, :pMeter, :pWeight, :pSellPrice,  :pCategory, :pPic)
-- attr_accessible(:pName, :pQuantity, :pMeter, :pWeight, :pSellPrice, :pCategory, :pPic)
rake aborted!
NoMethodError: undefined method `attr_accessible' for #<ActiveRecord::Migration:0x007f2dc2ba45b8>
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:648:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:641:in `method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:406:in `method_missing'
/app/db/migrate/20150802134246_create_inventories.rb:2:in `<class:CreateInventories>'
/app/db/migrate/20150802134246_create_inventories.rb:1:in `<top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:761:in `load_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:757:in `migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `disable_ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1044:in `use_transaction?'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:954:in `rescue in block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:951:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate

我一直在试图找出原因,在想了想之后我发现 rails 4.0.0 的变化不再使用 attr_accessible 我们应该使用 strong 参数,所以从模型中删除 attr_accessible 将解决问题...

但是,我有一个空模型,我看的任何地方都没有 attr_accessible。 (除此之外,为什么我的应用程序在本地运行而不是在 heroku 上运行很奇怪?) 我无法弄清楚为什么会出现此错误以及在哪里寻找解决方案。我一直在尝试查看 active_record 文件,但害怕做出任何更改,有什么想法吗?

另外,谁能告诉我任何可以帮助我阅读此类日志错误的资源?我试图阅读一些文章,但找不到像我这样的菜鸟容易理解的文章...... ;(

【问题讨论】:

  • 您似乎在迁移文件中使用了attr_accessible。打开/app/db/migrate/20150802134246_create_inventories.rb,看看里面有什么。
  • 您可能不小心添加/修改了“旧”迁移文件。您从未在本地看到影响,因为 db:migrate 只会在本地运行“新”迁移,但如果没有在 heroku 上运行迁移,则会遇到错误
  • @XMLSlayer 哇!谢啦!有用!之前没注意!!你一天更安全!
  • @mtamhankar 感谢您的解释!

标签: ruby-on-rails ruby ruby-on-rails-3 heroku attr-accessible


【解决方案1】:

很高兴我能帮忙:)

就阅读日志错误而言,这就是我所做的。你会得到一堆关于你从未接触过的文件的行。这是因为虽然写的东西很可能有错误,但错误会传播到整个 Rails 框架。这就是 Rails 非常酷的原因:它为你做了很多事情,所以你不必担心它们。

如果错误出现在您编写的代码中,该文件也应该列在堆栈跟踪中。所以在这个例子中,你有一堆/app/vendor/bundle/...(我假设你没有碰过),但是还有一个/app/db/migrate/...(很可能是你创建的)。

这让我认为错误在那个文件中。另外,它甚至给你一个行号(这就是:2create_inventories.rb:2 中的内容)。当然,这些行号并不总是正确的,但它应该让你从正确的地方开始!

【讨论】:

    猜你喜欢
    • 2012-09-06
    • 2012-11-23
    • 1970-01-01
    • 2017-10-04
    • 2016-03-08
    • 2013-03-08
    • 2017-04-16
    • 2012-08-03
    • 1970-01-01
    相关资源
    最近更新 更多