在我的项目中,我使用游戏和ticker_activites 之间的一对多关系,就像这样
模型
class Game < ActiveRecord::Base
has_many :ticker_activities, :dependent => :destroy, :order => 'time ASC'
end
class TickerActivity < ActiveRecord::Base
belongs_to :game
end
删除游戏时...
看法
<td><%= link_to 'Destroy', game, confirm: 'Are you sure?', method: :delete %></td>
控制器
def destroy
@game = Game.find(params[:id])
@game.destroy <-------error highlights on this line
end
..我收到这个错误...
nil:NilClass 的未定义方法“名称”
参数
{"_method"=>"delete",
"authenticity_token"=>"684Odq56+B...",
"id"=>"20"}
...并且游戏不会在生产中被删除(Heroku)。
我正在使用 Ruby 2.2.4 和 Rails 3.2.20。
任何想法如何解决这个问题?
编辑: 我的ticker_activity.rb:
class TickerActivity < ActiveRecord::Base
belongs_to :game
belongs_to :player
belongs_to :team
def self.convert_seconds_to_time(seconds)
total_minutes = seconds / 1.minutes
seconds_in_last_minute = seconds - total_minutes.minutes.seconds
"#{sprintf '%02d', total_minutes}:#{sprintf '%02d', seconds_in_last_minute}"
end
# Import CSV-File
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
ticker_hash = row.to_hash
TickerActivity.create!(ticker_hash)
end
end
end
全跟踪
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:58:in `cached_counter_attribute_name'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:81:in `inverse_updates_counter_cache?'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:91:in `delete_records'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:466:in `remove_records'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:459:in `block in delete_or_destroy'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:149:in `block in transaction'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/transactions.rb:208:in `transaction'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:148:in `transaction'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:459:in `delete_or_destroy'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:231:in `delete'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_association.rb:157:in `delete_all'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/collection_proxy.rb:46:in `delete_all'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/builder/has_many.rb:42:in `block in define_destroy_dependency_method'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:407:in `_run__4425773486301104750__destroy__2967777598099461333__callbacks'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:385:in `_run_destroy_callbacks'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/callbacks.rb:254:in `destroy'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/transactions.rb:254:in `block in destroy'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/transactions.rb:208:in `transaction'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/transactions.rb:254:in `destroy'
app/controllers/games_controller.rb:77:in `destroy'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/abstract_controller/base.rb:167:in `process_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/rendering.rb:10:in `process_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:414:in `_run__924265301303643032__process_action__1711436731218752401__callbacks'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/abstract_controller/callbacks.rb:17:in `process_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/rescue.rb:29:in `process_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/notifications.rb:123:in `block in instrument'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/notifications.rb:123:in `instrument'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
vendor/bundle/ruby/2.2.0/gems/searchkick-1.3.0/lib/searchkick/logging.rb:153:in `process_action'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/abstract_controller/base.rb:121:in `process'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/abstract_controller/rendering.rb:45:in `process'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal.rb:203:in `dispatch'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_controller/metal.rb:246:in `block in action'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/routing/route_set.rb:73:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/routing/route_set.rb:36:in `call'
vendor/bundle/ruby/2.2.0/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
vendor/bundle/ruby/2.2.0/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
vendor/bundle/ruby/2.2.0/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/routing/route_set.rb:608:in `call'
vendor/bundle/ruby/2.2.0/gems/warden-1.2.6/lib/warden/manager.rb:35:in `block in call'
vendor/bundle/ruby/2.2.0/gems/warden-1.2.6/lib/warden/manager.rb:34:in `catch'
vendor/bundle/ruby/2.2.0/gems/warden-1.2.6/lib/warden/manager.rb:34:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/etag.rb:23:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/conditionalget.rb:35:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/head.rb:14:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/flash.rb:242:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/session/abstract/id.rb:210:in `context'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/session/abstract/id.rb:205:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/cookies.rb:341:in `call'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/query_cache.rb:64:in `call'
vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:405:in `_run__2936395425807340860__call__2967777598099461333__callbacks'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:405:in `__run_callback'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/callbacks.rb:81:in `run_callbacks'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
vendor/bundle/ruby/2.2.0/gems/railties-3.2.20/lib/rails/rack/logger.rb:32:in `call_app'
vendor/bundle/ruby/2.2.0/gems/railties-3.2.20/lib/rails/rack/logger.rb:16:in `block in call'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/tagged_logging.rb:22:in `tagged'
vendor/bundle/ruby/2.2.0/gems/railties-3.2.20/lib/rails/rack/logger.rb:16:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/request_id.rb:22:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/methodoverride.rb:21:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/runtime.rb:17:in `call'
vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.20/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-1.4.7/lib/rack/lock.rb:15:in `call'
vendor/bundle/ruby/2.2.0/gems/actionpack-3.2.20/lib/action_dispatch/middleware/static.rb:82:in `call'
vendor/bundle/ruby/2.2.0/gems/rack-cache-1.6.1/lib/rack/cache/context.rb:140:in `forward'
vendor/bundle/ruby/2.2.0/gems/rack-cache-1.6.1/lib/rack/cache/context.rb:147:in `pass'
vendor/bundle/ruby/2.2.0/gems/rack-cache-1.6.1/lib/rack/cache/context.rb:159:in `invalidate'
vendor/bundle/ruby/2.2.0/gems/rack-cache-1.6.1/lib/rack/cache/context.rb:74:in `call!'
vendor/bundle/ruby/2.2.0/gems/rack-cache-1.6.1/lib/rack/cache/context.rb:51:in `call'
vendor/bundle/ruby/2.2.0/gems/railties-3.2.20/lib/rails/engine.rb:484:in `call'
vendor/bundle/ruby/2.2.0/gems/railties-3.2.20/lib/rails/application.rb:231:in `call'
vendor/bundle/ruby/2.2.0/gems/railties-3.2.20/lib/rails/railtie/configurable.rb:30:in `method_missing'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/connection.rb:86:in `block in pre_process'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/connection.rb:84:in `catch'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/connection.rb:84:in `pre_process'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/connection.rb:53:in `process'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/connection.rb:39:in `receive_data'
vendor/bundle/ruby/2.2.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in `run_machine'
vendor/bundle/ruby/2.2.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in `run'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/backends/base.rb:73:in `start'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/server.rb:162:in `start'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/controllers/controller.rb:87:in `start'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/runner.rb:200:in `run_command'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/lib/thin/runner.rb:156:in `run!'
vendor/bundle/ruby/2.2.0/gems/thin-1.7.0/bin/thin:6:in `<top (required)>'
vendor/bundle/ruby/2.2.0/bin/thin:23:in `load'
vendor/bundle/ruby/2.2.0/bin/thin:23:in `<main>'
错误日志
2016-06-21T07:15:01.216797+00:00 app[web.1]: Started DELETE "/games/21" for 176.0.32.209 at 2016-06-21 07:15:01 +0000
2016-06-21T07:15:01.223899+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:53: warning: circular argument reference - reflection
2016-06-21T07:15:01.223919+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:57: warning: circular argument reference - reflection
2016-06-21T07:15:01.223948+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:61: warning: circular argument reference - reflection
2016-06-21T07:15:01.254045+00:00 app[web.1]:
2016-06-21T07:15:01.254042+00:00 app[web.1]: NoMethodError (undefined method `name' for nil:NilClass):
2016-06-21T07:15:01.254045+00:00 app[web.1]:
2016-06-21T07:15:01.254044+00:00 app[web.1]: app/controllers/games_controller.rb:77:in `destroy'
2016-06-21T07:15:01.254004+00:00 app[web.1]:
2016-06-21T07:15:01.224044+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.20/lib/active_record/associations/has_many_association.rb:80: warning: circular argument reference - reflection