耙子流产了! NoMethodError:迁移到Rails 4.0时的未定义方法

时间:2016-06-27 12:40:18

标签: ruby-on-rails

我正在开发从3.2.13升级到4.0.0的Rails应用程序,在运行应用程序之前,我需要运行一个种子文件:

seeds.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来自哪里。我搜索了整个应用程序。请帮帮我。

1 个答案:

答案 0 :(得分:0)

在rails 4中将find_or_create_by_name更改为find_or_create_by