Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove active record object from array

Tags:

I have set of active record object in array.

I just want to delete on object from array not in database

a = Model.limit(2)  b = Model.first  a.delete(b) 

returning nil value

Its not deleting

is there anyway?

like image 414
sangeethkumar Avatar asked Jun 02 '13 14:06

sangeethkumar


1 Answers

a.to_a - [b] 

Background: a.to_a convertrs the relation into an array in in memory.
[b] is an array whith just the element, you want to delete (in memory).
a.to_a - [b] does an array substraction.

(In Rails 3.2 .to_a was applied automatically to a relation when it was accessed. I agree with gregates: It's better to convert the relation to an array explicitly)

like image 191
Martin M Avatar answered Oct 20 '22 03:10

Martin M