【问题标题】:rake aborted! NoMethodError: undefined method when migrated to Rails 4.0耙中止! NoMethodError:迁移到 Rails 4.0 时未定义的方法
【发布时间】:2016-06-27 12:40:17
【问题描述】:

我正在将 Rails 应用程序从 3.2.13 迁移到 4.0.0,在运行应用程序之前,我需要运行一个种子文件:

种子.rb

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
#   Mayor.create(:name => 'Daley', :city => cities.first)

# Create subscription plans
    SubscriptionPlan.find_or_create_by_name(name: "free", amount: 0.00, renewal_period: 1, trial_period: 30, description: 'Free Plan')
    SubscriptionPlan.find_or_create_by_name(name: "year", amount: 149.00, renewal_period: 12, trial_period: 30, description: "Year Plan")

这是桌子:

create_table "subscription_plans", :force => true do |t|
      t.string   "name"
      t.decimal  "amount",         :precision => 10, :scale => 2
      t.integer  "renewal_period",                                :default => 1
      t.integer  "trial_period",                                  :default => 1
      t.text     "description"
end

在运行 rake db:seed 时,这是我在当前版本中遇到的错误,而在以前的版本中它很好。错误:

rake aborted! NoMethodError: undefined method `trial_interval' for
#<SubscriptionPlan:0x0000000622b0b8> Did you mean?  trial_period /home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in
`method_missing'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in
`method_missing'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validator.rb:151:in `block in validate'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `each'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `validate'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:283:in
`_callback_before_39'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:407:in
`_run__3876620741187521333__validate__callbacks'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in
`run_callbacks'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validations.rb:373:in `run_validations!'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in
`block in run_validations!'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in
`_run__3876620741187521333__validation__callbacks'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in
`run_callbacks'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in
`run_validations!'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activemodel-4.0.0/lib/active_model/validations.rb:314:in `valid?'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/validations.rb:70:in
`valid?'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/validations.rb:77:in
`perform_validations'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/validations.rb:51:in
`save'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:32:in
`save'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in
`block (2 levels) in save'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in
`block in with_transaction_returning_status'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
`block in transaction'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in
`within_new_transaction'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
`transaction'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in
`transaction'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in
`with_transaction_returning_status'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in
`block in save'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:281:in
`rollback_active_record_state!'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/transactions.rb:269:in
`save'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/protected_attributes-1.0.3/lib/active_record/mass_assignment_security/persistence.rb:46:in `create'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/relation.rb:121:in
`block in create'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in
`scoping'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/relation.rb:121:in
`create'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-deprecated_finders-1.0.4/lib/active_record/deprecated_finders/dynamic_matchers.rb:141:in `dispatch'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:67:in
`find_or_create_by_name'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:20:in
`method_missing'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/attr_encrypted-1.2.1/lib/attr_encrypted/adapters/active_record.rb:50:in
`method_missing_with_attr_encrypted'
/home/abc/Desktop/Proj2/db/seeds.rb:10:in `<top (required)>'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in
`load'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in
`block in load'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in
`load_dependency'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in
`load'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/railties-4.0.0/lib/rails/engine.rb:540:in
`load_seed'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:153:in
`load_seed'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:181:in
`block (2 levels) in <top (required)>'
/home/abc/.rvm/gems/ruby-2.3.0@proj/gems/rake-11.2.2/exe/rake:27:in
`<top (required)>'
/home/abc/.rvm/gems/ruby-2.3.0@proj/bin/ruby_executable_hooks:15:in
`eval'
/home/abc/.rvm/gems/ruby-2.3.0@proj/bin/ruby_executable_hooks:15:in
`<main>' Tasks: TOP => db:seed (See full trace by running task with
--trace)

我不知道这个 trial_interval 是从哪里来的。我已经搜索了整个应用程序。请帮帮我。

【问题讨论】:

  • 你可以发布你的模型吗?
  • 好像有人遇到过类似的问题:stackoverflow.com/questions/13537722/…
  • 听起来您对该字段进行了验证
  • 你提供的日志说:“/home/abc/Desktop/Proj2/db/seeds.rb:10”,所以你的文件seeds.rb中有超过2行?跨度>
  • @oliviergg - 剩余数据运行良好,只有这些订阅计划没有运行。

标签: ruby-on-rails


【解决方案1】:

只需在 rails 4 中将 find_or_create_by_name 更改为 find_or_create_by

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多