gpt4 book ai didi

ruby-on-rails - RoR : NoMethodError when executing delete_all for a has_many relationship

转载 作者:行者123 更新时间:2023-12-02 15:28:01 26 4
gpt4 key购买 nike

我的类如下,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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com