我的類如下所示,其中Customer使用單表繼承方法從User繼承。用戶具有屬性名稱和電子郵件,而訂單有目的地。基於代碼運行下面的代碼片段RoR:對has_many關係執行delete_all時的NoMethodError
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:對於無未定義的方法「名」:NilClass。但是,下面的作品。
c = Customer.new
c.orders << Order.create
c.orders.delete_all
同樣的作品完美地發現朋友的電腦上。任何人都知道可能會發生什麼?可能它與我正在使用的某個版本中的某個錯誤有關?
堆棧跟蹤
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>'
你能確切知道'name'是哪個模型的屬性嗎?看起來錯誤不是來自於delete_all方法 – Chambeur 2015-04-02 09:29:22
您能否給出一個異常的堆棧跟蹤以及User,Customer和Order的完整源代碼。似乎有一些積極的記錄回調或驗證問題。 'new'和'create'之間的區別在於'create'立即運行所有回調,驗證並將對象保存在數據庫中,而'new'只在內存中創建對象。 – 2015-04-02 09:42:52
@Chambeur我上面已經說明了,但也許我還不夠清楚。 用戶欄位=>姓名,電郵 訂單欄位=>目的地 – keylime 2015-04-02 10:29:29