[英]Rails error: undefined method `push' for nil:NilClass
所以我有以下代碼:
new = @params[collection.to_s + '_attributes']
old = @model.send collection
if new.nil?
old.clear
else
new_records = new.map { |_, e| e[:id] }
if !new_records.nil? && !old.nil?
old.not_in(id: new_records).destroy_all
end
end
問題是我沒有在代碼中的任何地方使用“推”功能,並且基於堆棧跟蹤,執行時發生錯誤:
old.not_in(id: new_records).destroy_all
我是Rails的新手,所以希望有人能幫助我。 提前致謝!
更新
我現在最終使用delete_all而不是destroy_all。 我認為這是導致錯誤的原因。 它現在正在工作,但是如果我能找出為什么它不能與destroy_all一起工作,那真是太好了。
我不認為not_in
是rails命令,而是嘗試
old.where.not(id: new_records).destroy_all
或not in
這樣使用。
old.where('id NOT IN (?)',new_records).destroy_all
嘗試:
old.where.not(id: [new_records]).destroy_all
並非總是需要數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.