Rails Money迁移失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails Money迁移失败相关的知识,希望对你有一定的参考价值。

我正在尝试导入所有迁移。但是,在create_table块中使用t.monetize :total, currency: { present: false }似乎崩溃,并出现以下错误消息:

class AddAddressAndOrder < ActiveRecord::Migration[5.1]
  def change
    create_table :line_items do |t|
      t.belongs_to :order
      t.integer :quantity, default: 0
      t.monetize :price, currency: { present: false }
      t.monetize :total, currency: { present: false }
      t.timestamps
    end
  end
end


*** ArgumentError Exception: wrong number of arguments (given 1, expected 2..3)

每次我进入t.monetize(或t.money)列引用时,我都会收到相同的错误...在下面添加(包括--trace):

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate

== 20160901101357 AddAddressAndOrder: migrating ===============================
-- create_table(:orders)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

wrong number of arguments (given 1, expected 2..3)
schema_definitions.rb:311:in `column'
table_pg_rails4.rb:9:in `block in monetize'
table_pg_rails4.rb:6:in `each'
table_pg_rails4.rb:6:in `monetize'
/Users/Paul/folder/Sites/project/db/migrate/20160901101357_add_address_and_order.rb:20:in `block in change'
schema_statements.rb:282:in `create_table'
abstract_mysql_adapter.rb:363:in `create_table'
method_missing'
say_with_time'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/benchmark.rb:293:in `measure'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `say_with_time'
`method_missing'
/Users/Paul/folder/Sites/project/db/migrate/20160901101357_add_address_and_order.rb:18:in `change'
`exec_migration'
migrate'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/benchmark.rb:293:in `measure'
migrate'
connection_pool.rb:408:in `with_connection'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:774:in `migrate'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:953:in `migrate'
execute_migration_in_transaction'
`ddl_transaction'
`execute_migration_in_transaction'
migrate_without_lock'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `each'
`migrate_without_lock'
migrate'
`with_advisory_lock'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:1148:in `migrate'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate'
`migrate'
levels) in <top (required)>'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `block in execute'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `each'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `execute'
invoke_with_call_chain'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
`invoke_with_call_chain'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:181:in `invoke'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:160:in `invoke_task'
top_level'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:116:in `each'
top_level'
`run_with_threads'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:110:in `top_level'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:83:in `block in run'
`standard_exception_handling'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:80:in `run'
/Users/Paul/.rvm/gems/ruby-2.5.0@global/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/Paul/.rvm/rubies/ruby-2.5.0/bin/rake:23:in `load'
/Users/Paul/.rvm/rubies/ruby-2.5.0/bin/rake:23:in `<main>'

Caused by:
ArgumentError: wrong number of arguments (given 1, expected 2..3)
schema_definitions.rb:311:in `column'
table_pg_rails4.rb:9:in `block in monetize'
table_pg_rails4.rb:6:in `each'
table_pg_rails4.rb:6:in `monetize'
/Users/Paul/folder/Sites/project/db/migrate/20160901101357_add_address_and_order.rb:20:in `block in change'
schema_statements.rb:282:in `create_table'
abstract_mysql_adapter.rb:363:in `create_table'
method_missing'
say_with_time'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/benchmark.rb:293:in `measure'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:818:in `say_with_time'
`method_missing'
/Users/Paul/folder/Sites/project/db/migrate/20160901101357_add_address_and_order.rb:18:in `change'
`exec_migration'
migrate'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/benchmark.rb:293:in `measure'
migrate'
connection_pool.rb:408:in `with_connection'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:774:in `migrate'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:953:in `migrate'
execute_migration_in_transaction'
`ddl_transaction'
`execute_migration_in_transaction'
migrate_without_lock'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:1200:in `each'
`migrate_without_lock'
migrate'
`with_advisory_lock'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:1148:in `migrate'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up'
/Users/Paul/.rvm/gems/ruby-2.5.0@project/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate'
`migrate'
levels) in <top (required)>'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `block in execute'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `each'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:251:in `execute'
invoke_with_call_chain'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
`invoke_with_call_chain'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb:181:in `invoke'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:160:in `invoke_task'
top_level'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:116:in `each'
top_level'
`run_with_threads'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:110:in `top_level'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:83:in `block in run'
`standard_exception_handling'
/Users/Paul/.rvm/rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb:80:in `run'
/Users/Paul/.rvm/gems/ruby-2.5.0@global/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/Paul/.rvm/rubies/ruby-2.5.0/bin/rake:23:in `load'
/Users/Paul/.rvm/rubies/ruby-2.5.0/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

关于Money-Rails,我的Gemfile.lock看起来像这样:

money (~> 6.10.1)
money-rails (1.10.0)
  activesupport (>= 3.0)
  monetize (~> 1.7.0)
  money (~> 6.10.0)
  railties (>= 3.0)

文档(https://github.com/RubyMoney/money-rails#migration-helpers)声明这应该没问题。所以我不知道该怎么做。

问题似乎在MoneyRails::ActiveRecord::Migration::Table.monetize方法中:

module MoneyRails
  module ActiveRecord
    module MigrationExtensions
      module Table
        def monetize(accessor, options={})
          [:amount, :currency].each do |attribute|
            column_present, _, *opts = OptionsExtractor.extract attribute, :no_table, accessor, options
            column(*opts) if column_present
          end
        end

        # Removed for brevity
      end
    end
  end
end

无论通过什么传递给货币化,它似乎总是抛出同样的错误。我真的很想知道我做错了什么,所以任何帮助都会非常感激。

答案

你在初学者中做了什么,可能会影响MoneyRails::Configuration#amount_column?看起来OptionsExtractor缺少一些东西。

以上是关于Rails Money迁移失败的主要内容,如果未能解决你的问题,请参考以下文章

Money-rails Gem 货币符号在某些设备上的重叠问题

添加设计生成的用户表时,Rails 数据库迁移失败并出现“重复的列名:电子邮件”

Android NavHostFragment(片段)膨胀失败,ViewBinding(使用导航组件)

money-rails 设置自定义货币

使用 lambda 或 proc 和 rails-money

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?