简体   繁体   English

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

[英]Rails error: undefined method `push' for nil:NilClass

So I have the following code: 所以我有以下代码:

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

The problem is I didn't use 'push' function anywhere in my code and based on the stacktrace the error occurs when executing: 问题是我没有在代码中的任何地方使用“推”功能,并且基于堆栈跟踪,执行时发生错误:

old.not_in(id: new_records).destroy_all

I'm new to Rails so I hope someone can help me. 我是Rails的新手,所以希望有人能帮助我。 Thanks in advance! 提前致谢!

UPDATE 更新

I ended up using delete_all instead of destroy_all for now. 我现在最终使用delete_all而不是destroy_all。 I think it was causing the error. 我认为这是导致错误的原因。 It's working now but it would really be nice if I could find out why it wasn't working with destroy_all. 它现在正在工作,但是如果我能找出为什么它不能与destroy_all一起工作,那真是太好了。

I don't think not_in is rails command, Instead, try 我不认为not_in是rails命令,而是尝试

old.where.not(id: new_records).destroy_all

or, not in can be used like this. not in这样使用。

old.where('id NOT IN (?)',new_records).destroy_all

Try: 尝试:

old.where.not(id: [new_records]).destroy_all

Not in always requires array. 并非总是需要数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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