RoR:为 has_many 关系执行 delete_all 时 NoMethodError [英] RoR: NoMethodError when executing delete_all for a has_many relationship

查看:40
本文介绍了RoR:为 has_many 关系执行 delete_all 时 NoMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类如下,Customer 使用单表继承方式从 User 继承.用户具有名称和电子邮件属性,而订单具有目的地.

My classes are as follows, with Customer inheriting from User using the single-table inheritance approach. User has attributes name and email while Order has destination.

class User < ActiveRecord::Base
end

class Customer < User
  has_many :orders
end

class Order < ActiveRecord::Base
  belongs_to :customer
end

基于以下使用 Rails 控制台运行的代码片段

Based on the following snippet of code run using the Rails console

c = Customer.create
c.orders << Order.create
c.orders.delete_all

在最后一行执行 delete_all 函数会导致 NoMethodError: undefined method 'name' for nil:NilClass.但是,以下有效.

Executing the delete_all function in the last line results in NoMethodError: undefined method 'name' for nil:NilClass. However, the following works.

c = Customer.new
c.orders << Order.create
c.orders.delete_all

在朋友的计算机上完全可以找到相同的作品.任何人都知道可能会发生什么?可能与我使用的某个版本的错误有关吗?

The same works perfectly find on a friend's computer. Anyone have any idea what might be going on? Might it be related to a bug in the version of something I'm using?

堆栈跟踪

NoMethodError: undefined method `name' for nil:NilClass
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-    4.0.3/lib/active_record/associations/has_many_association.rb:81:in `cached_counter_attribute_name'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/has_many_association.rb:77:in `has_cached_counter?'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/has_many_association.rb:85:in `update_counter'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/has_many_association.rb:125:in `delete_records'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:493:in `remove_records'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:486:in `block in delete_or_destroy'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:152:in `block in transaction'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/transactions.rb:209:in `transaction'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:151:in `transaction'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:486:in `delete_or_destroy'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:230:in `delete'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_association.rb:160:in `delete_all'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.0.3/lib/active_record/associations/collection_proxy.rb:422:in `delete_all'
from (irb):6
from /home/leo/.rvm/gems/ruby-2.2.1/gems/railties-4.0.3/lib/rails/commands/console.rb:90:in `start'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/railties-4.0.3/lib/rails/commands/console.rb:9:in `start'
from /home/leo/.rvm/gems/ruby-2.2.1/gems/railties-4.0.3/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

推荐答案

我想你对 ruby​​ 版本的问题是正确的.我发现了这个问题.

I guess you were right about the problem about the ruby version. I found this question.

这家伙遇到了类似的问题,他通过将他的 ruby​​ 版本从 ruby​​-2.2.0 切换到 ruby​​-2.1.1 解决了这个问题.

The guy had a similar problem and he fixed it by switching his ruby version from ruby-2.2.0 to ruby-2.1.1.

如果不是 ruby​​ 版本,则可能是 ActiveRecord 版本.

If it's not the ruby version, it could be the ActiveRecord Version.

这篇关于RoR:为 has_many 关系执行 delete_all 时 NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆