0

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. Thanks in advance!

UPDATE

I ended up using delete_all instead of destroy_all for now. 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.

4
  • What are you trying to achieve with this piece of code? What is the code for not_in? Commented Jan 17, 2017 at 4:19
  • I'm not really sure too. Sorry for the incomplete details. I actually thought not_in is a built-in function. Commented Jan 17, 2017 at 4:44
  • I don't think not_in is rails command, try old.where.not(id: new_records) .destroy_all Commented Jan 17, 2017 at 4:46
  • 1
    If you have a stacktrace, you should add it to the question. Commented Jan 17, 2017 at 4:51

2 Answers 2

1

I don't think not_in is rails command, Instead, try

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

or, not in can be used like this.

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try:

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

Not in always requires array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.