不能为BigDecimal定义单例方法“encode_with”

时间:2011-06-30 05:42:43

标签: ruby-on-rails ruby-on-rails-3

尝试更新我的某个模型时,我得到异常can't define singleton method "encode_with" for BigDecimal。我正在使用Rails 3.0.7& Ruby 1.9.2。

SkateparksController

def update
  @skatepark.update_attributes(params[:skatepark])
end

完整跟踪

/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/deprecated.rb:16:in `singleton_method_added'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/deprecated.rb:16:in `define_method'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/deprecated.rb:16:in `quick_emit'
activesupport (3.0.7) lib/active_support/core_ext/big_decimal/conversions.rb:15:in `to_yaml'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:55:in `accept'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:227:in `block in visit_Hash'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:225:in `each'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:225:in `visit_Hash'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in `accept'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in `<<'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych.rb:165:in `dump'
rspec-mocks (2.6.0) lib/rspec/mocks/extensions/psych.rb:5:in `dump_with_mocks'
/Users/Kyle/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/psych/core_ext.rb:13:in `psych_to_yaml'
paper_trail (2.2.5) lib/paper_trail/has_paper_trail.rb:158:in `object_to_string'
paper_trail (2.2.5) lib/paper_trail/has_paper_trail.rb:113:in `record_update'
activesupport (3.0.7) lib/active_support/callbacks.rb:415:in `_run_update_callbacks'
activerecord (3.0.7) lib/active_record/callbacks.rb:281:in `update'
activerecord (3.0.7) lib/active_record/persistence.rb:246:in `create_or_update'
activerecord (3.0.7) lib/active_record/callbacks.rb:273:in `block in create_or_update'
activesupport (3.0.7) lib/active_support/callbacks.rb:444:in `_run_save_callbacks'
activerecord (3.0.7) lib/active_record/callbacks.rb:273:in `create_or_update'
activerecord (3.0.7) lib/active_record/persistence.rb:39:in `save'
activerecord (3.0.7) lib/active_record/validations.rb:43:in `save'
activerecord (3.0.7) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (3.0.7) lib/active_record/transactions.rb:240:in `block (2 levels) in save'
activerecord (3.0.7) lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:207:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/transactions.rb:240:in `block in save'
activerecord (3.0.7) lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
activerecord (3.0.7) lib/active_record/transactions.rb:239:in `save'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:245:in `block in update_address'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:9:in `without_callback'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:244:in `update_address'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:218:in `block in attach_geocode'
activesupport (3.0.7) lib/active_support/callbacks.rb:414:in `_run_geocoding_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:216:in `attach_geocode'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:151:in `block in validates_as_geocodable'
activesupport (3.0.7) lib/active_support/callbacks.rb:470:in `_run_validate_callbacks'
activemodel (3.0.7) lib/active_model/validations.rb:212:in `run_validations!'
activemodel (3.0.7) lib/active_model/validations/callbacks.rb:67:in `block in run_validations!'
activesupport (3.0.7) lib/active_support/callbacks.rb:424:in `_run_validation_callbacks'
activemodel (3.0.7) lib/active_model/validations/callbacks.rb:67:in `run_validations!'
activemodel (3.0.7) lib/active_model/validations.rb:179:in `valid?'
activerecord (3.0.7) lib/active_record/validations.rb:55:in `valid?'
activerecord (3.0.7) lib/active_record/validations.rb:75:in `perform_validations'
activerecord (3.0.7) lib/active_record/validations.rb:43:in `save'
activerecord (3.0.7) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (3.0.7) lib/active_record/transactions.rb:240:in `block (2 levels) in save'
activerecord (3.0.7) lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:207:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/transactions.rb:240:in `block in save'
activerecord (3.0.7) lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
activerecord (3.0.7) lib/active_record/transactions.rb:239:in `save'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:245:in `block in update_address'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:9:in `without_callback'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:244:in `update_address'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:218:in `block in attach_geocode'
activesupport (3.0.7) lib/active_support/callbacks.rb:414:in `_run_geocoding_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:216:in `attach_geocode'
acts_as_geocodable (2.0.2) lib/acts_as_geocodable.rb:151:in `block in validates_as_geocodable'
activesupport (3.0.7) lib/active_support/callbacks.rb:470:in `_run_validate_callbacks'
activemodel (3.0.7) lib/active_model/validations.rb:212:in `run_validations!'
activemodel (3.0.7) lib/active_model/validations/callbacks.rb:67:in `block in run_validations!'
activesupport (3.0.7) lib/active_support/callbacks.rb:424:in `_run_validation_callbacks'
activemodel (3.0.7) lib/active_model/validations/callbacks.rb:67:in `run_validations!'
activemodel (3.0.7) lib/active_model/validations.rb:179:in `valid?'
activerecord (3.0.7) lib/active_record/validations.rb:55:in `valid?'
activerecord (3.0.7) lib/active_record/validations.rb:75:in `perform_validations'
activerecord (3.0.7) lib/active_record/validations.rb:43:in `save'
activerecord (3.0.7) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (3.0.7) lib/active_record/transactions.rb:240:in `block (2 levels) in save'
activerecord (3.0.7) lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:207:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/transactions.rb:240:in `block in save'
activerecord (3.0.7) lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
activerecord (3.0.7) lib/active_record/transactions.rb:239:in `save'
activerecord (3.0.7) lib/active_record/persistence.rb:128:in `block in update_attributes'
activerecord (3.0.7) lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:207:in `transaction'
activerecord (3.0.7) lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
activerecord (3.0.7) lib/active_record/persistence.rb:126:in `update_attributes'
app/controllers/admin/skateparks_controller.rb:54:in `update'
actionpack (3.0.7) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.7) lib/active_support/callbacks.rb:472:in `block in _run__3889911993370107397__process_action__388168845574438513__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:221:in `block in _conditional_callback_around_218'
activesupport (3.0.7) lib/active_support/callbacks.rb:331:in `around'
activesupport (3.0.7) lib/active_support/callbacks.rb:315:in `_callback_around_33'
activesupport (3.0.7) lib/active_support/callbacks.rb:220:in `_conditional_callback_around_218'
activesupport (3.0.7) lib/active_support/callbacks.rb:436:in `_run__3889911993370107397__process_action__388168845574438513__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.7) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:82:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:493:in `call'
oa-core (0.2.6) lib/omniauth/strategy.rb:44:in `call!'
oa-core (0.2.6) lib/omniauth/strategy.rb:30:in `call'
oa-core (0.2.6) lib/omniauth/strategy.rb:44:in `call!'
oa-core (0.2.6) lib/omniauth/strategy.rb:30:in `call'
oa-core (0.2.6) lib/omniauth/strategy.rb:44:in `call!'
oa-core (0.2.6) lib/omniauth/strategy.rb:30:in `call'
oa-core (0.2.6) lib/omniauth/builder.rb:30:in `call'
sass (3.1.3) lib/sass/plugin/rack.rb:54:in `call'
warden (1.0.4) lib/warden/manager.rb:35:in `block in call'
warden (1.0.4) lib/warden/manager.rb:34:in `catch'
warden (1.0.4) lib/warden/manager.rb:34:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.3) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
flash_cookie_session (1.1.1) lib/flash_cookie_session/middleware.rb:18:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.7) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.7) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/chunked.rb:15:in `call'
thin (1.2.11) lib/thin/connection.rb:84:in `block in pre_process'
thin (1.2.11) lib/thin/connection.rb:82:in `catch'
thin (1.2.11) lib/thin/connection.rb:82:in `pre_process'
thin (1.2.11) lib/thin/connection.rb:57:in `process'
thin (1.2.11) lib/thin/connection.rb:42:in `receive_data'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
thin (1.2.11) lib/thin/backends/base.rb:61:in `start'
thin (1.2.11) lib/thin/server.rb:159:in `start'
rack (1.2.3) lib/rack/handler/thin.rb:14:in `run'
rack (1.2.3) lib/rack/server.rb:217:in `start'
railties (3.0.7) lib/rails/commands/server.rb:65:in `start'
railties (3.0.7) lib/rails/commands.rb:30:in `block in <top (required)>'
railties (3.0.7) lib/rails/commands.rb:27:in `tap'
railties (3.0.7) lib/rails/commands.rb:27:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

2 个答案:

答案 0 :(得分:2)

可能是因为YAML解析器错误。我有一个类似的错误,我使用以下代码修复:

#in config/boot.rb
require 'rubygems'
require 'yaml'
YAML::ENGINE.yamler= 'syck'
# Set up gems listed in the Gemfile.
gemfile = File.expand_path('../../Gemfile', __FILE__)
......

我在ubuntu 10.10+机器上看到了这个错误

答案 1 :(得分:2)

我在Windows上使用jruby 1.6.5,ruby 1.9和rails 2.3.14时遇到了类似的问题

    $ jruby -v
    jruby 1.6.5 (ruby-1.9.2-p136) (2011-10-25 9dcd388) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [Windows 7-amd64-java]

通过查看rails 3的修复程序,我能够解决这个问题。我所要做的就是将一个文件(我称之为fix_big_decimal.rb)添加到我的config / initializers:

    module ActiveSupport
      module CoreExtensions
        module BigDecimal
          module Conversions
            def encode_with(coder)
              string = to_s
              coder.represent_scalar(nil, YAML_MAPPING[string] || string)
            end
            # Backport this method if it doesn't exist
            unless method_defined?(:to_d)
              def to_d
                self
              end
            end
          end   
        end
      end
    end