繁体   English   中英

Rails错误:nil:NilClass的未定义方法“ push”

[英]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.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM