作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的类如下,Customer使用单表继承方式从User继承。用户具有属性名称和电子邮件,而订单具有目的地。
class User < ActiveRecord::Base
end
class Customer < User
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
end
基于以下使用 Rails 控制台运行的代码片段
c = Customer.create
c.orders << Order.create
c.orders.delete_all
在最后一行执行 delete_all 函数会导致 NoMethodError: undefined method 'name' for nil:NilClass。但是,以下工作。
c = Customer.new
c.orders << Order.create
c.orders.delete_all
同样的作品在 friend 的电脑上完美找到。任何人都知道会发生什么?它可能与我正在使用的版本中的错误有关吗?
堆栈跟踪
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 版本的问题是正确的。我找到了这个 question .
这个人有一个类似的问题,他通过将他的 ruby 版本从 ruby-2.2.0 切换到 ruby-2.1.1 来解决这个问题。
如果不是ruby版本,可能是ActiveRecord版本。
关于ruby-on-rails - RoR : NoMethodError when executing delete_all for a has_many relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29409323/
我是一名优秀的程序员,十分优秀!