1

私はポリモーフィックモデルを持っています:

class Vote < ActiveRecord::Base
  belongs_to :voteable, :polymorphic => true

  after_destroy :sum_votes

  private

    def sum_votes
      voteable.update_column(:votes_sum, 0)
    end

end

投票を作成しようとすると、次のエラーが発生します。

undefined method `scoped' for Class:Class

これはメソッド内で発生しsum_votes、関連付けに起因しているようvoteableです。何が原因でしょうか?

アップデート:

App backtrace
-------------

 - app/models/vote.rb:44:in `sum_votes'
 - app/models/vote.rb:16:in `cast_vote_of_type'
 - app/models/tag/item/vote.rb:7:in `cast_vote'
 - app/controllers/tag/item/votes_controller.rb:6:in `create'

Full backtrace
--------------

 - (gem) activerecord-3.2.12/lib/active_record/associations/association.rb:123:in `target_scope'
 - (gem) activerecord-3.2.12/lib/active_record/associations/association.rb:87:in `scoped'
 - (gem) activerecord-3.2.12/lib/active_record/associations/singular_association.rb:42:in `find_target'
 - (gem) activerecord-3.2.12/lib/active_record/associations/association.rb:145:in `load_target'
 - (gem) activerecord-3.2.12/lib/active_record/associations/association.rb:55:in `reload'
 - (gem) activerecord-3.2.12/lib/active_record/associations/singular_association.rb:9:in `reader'
 - (gem) activerecord-3.2.12/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
 - app/models/vote.rb:44:in `sum_votes'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:427:in `_run__901513201__save__352223903__callbacks'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
 - (gem) activerecord-3.2.12/lib/active_record/callbacks.rb:264:in `create_or_update'
 - (gem) activerecord-3.2.12/lib/active_record/persistence.rb:84:in `save'
 - (gem) activerecord-3.2.12/lib/active_record/validations.rb:50:in `save'
 - (gem) activerecord-3.2.12/lib/active_record/attribute_methods/dirty.rb:22:in `save'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:259:in `block (2 levels) in save'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
 - (gem) activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:208:in `transaction'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:259:in `block in save'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
 - (gem) activerecord-3.2.12/lib/active_record/transactions.rb:258:in `save'
 - (gem) activerecord-3.2.12/lib/active_record/persistence.rb:45:in `create'
 - (gem) activerecord-3.2.12/lib/active_record/relation.rb:87:in `block in create'
 - (gem) activerecord-3.2.12/lib/active_record/relation.rb:241:in `block in scoping'
 - (gem) activerecord-3.2.12/lib/active_record/scoping.rb:98:in `with_scope'
 - (gem) activerecord-3.2.12/lib/active_record/relation.rb:241:in `scoping'
 - (gem) activerecord-3.2.12/lib/active_record/relation.rb:87:in `create'
 - (gem) activerecord-3.2.12/lib/active_record/relation.rb:119:in `first_or_create'
 - app/models/vote.rb:16:in `cast_vote_of_type'
 - app/models/tag/item/vote.rb:7:in `cast_vote'
 - app/controllers/tag/item/votes_controller.rb:6:in `create'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
 - (gem) actionpack-3.2.12/lib/abstract_controller/base.rb:167:in `process_action'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/rendering.rb:10:in `process_action'
 - (gem) actionpack-3.2.12/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:436:in `_run__348146889__process_action__388332341__callbacks'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
 - (gem) actionpack-3.2.12/lib/abstract_controller/callbacks.rb:17:in `process_action'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/rescue.rb:29:in `process_action'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
 - (gem) activesupport-3.2.12/lib/active_support/notifications.rb:123:in `block in instrument'
 - (gem) activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 - (gem) activesupport-3.2.12/lib/active_support/notifications.rb:123:in `instrument'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
 - (gem) activerecord-3.2.12/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - (gem) actionpack-3.2.12/lib/abstract_controller/base.rb:121:in `process'
 - (gem) actionpack-3.2.12/lib/abstract_controller/rendering.rb:45:in `process'
 - (gem) actionpack-3.2.12/lib/action_controller/metal.rb:203:in `dispatch'
 - (gem) actionpack-3.2.12/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
 - (gem) actionpack-3.2.12/lib/action_controller/metal.rb:246:in `block in action'
 - (gem) actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
 - (gem) actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:36:in `call'
 - (gem) journey-1.0.4/lib/journey/router.rb:68:in `block in call'
 - (gem) journey-1.0.4/lib/journey/router.rb:56:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:601:in `call'
 - (gem) meta_request-0.2.3/lib/meta_request/middlewares/app_request_handler.rb:11:in `call'
 - (gem) rack-contrib-1.1.0/lib/rack/contrib/response_headers.rb:17:in `call'
 - (gem) meta_request-0.2.3/lib/meta_request/middlewares/headers.rb:16:in `call'
 - (gem) meta_request-0.2.3/lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
 - (gem) warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
 - (gem) warden-1.2.1/lib/warden/manager.rb:34:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
 - (gem) rack-1.4.5/lib/rack/etag.rb:23:in `call'
 - (gem) rack-1.4.5/lib/rack/conditionalget.rb:35:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/head.rb:14:in `call'
 - (gem) remotipart-1.0.5/lib/remotipart/middleware.rb:30:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/flash.rb:242:in `call'
 - (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context'
 - (gem) rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/cookies.rb:341:in `call'
 - (gem) activerecord-3.2.12/lib/active_record/query_cache.rb:64:in `call'
 - (gem) activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `_run__936866828__call__352223903__callbacks'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
 - (gem) activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/reloader.rb:65:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
 - (gem) better_errors-0.7.2/lib/better_errors/middleware.rb:84:in `protected_app_call'
 - (gem) better_errors-0.7.2/lib/better_errors/middleware.rb:79:in `better_errors_call'
 - (gem) better_errors-0.7.2/lib/better_errors/middleware.rb:56:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
 - (gem) railties-3.2.12/lib/rails/rack/logger.rb:32:in `call_app'
 - (gem) railties-3.2.12/lib/rails/rack/logger.rb:16:in `block in call'
 - (gem) activesupport-3.2.12/lib/active_support/tagged_logging.rb:22:in `tagged'
 - (gem) railties-3.2.12/lib/rails/rack/logger.rb:16:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/request_id.rb:22:in `call'
 - (gem) rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
 - (gem) rack-1.4.5/lib/rack/runtime.rb:17:in `call'
 - (gem) activesupport-3.2.12/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
 - (gem) rack-1.4.5/lib/rack/lock.rb:15:in `call'
 - (gem) actionpack-3.2.12/lib/action_dispatch/middleware/static.rb:62:in `call'
 - (gem) railties-3.2.12/lib/rails/engine.rb:479:in `call'
 - (gem) railties-3.2.12/lib/rails/application.rb:223:in `call'
 - (gem) rack-1.4.5/lib/rack/content_length.rb:14:in `call'
 - (gem) railties-3.2.12/lib/rails/rack/log_tailer.rb:17:in `call'
 - (gem) rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
 - C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
 - C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
 - C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
4

0 に答える 0