0

I have a collection of AR objects in a loop, that will likely be run many times, so I want to empty the collection at the end of each iteration.

What's the most efficient way to do that?

Edit 1

Here is an example of what happens when I use .clear:

1.9.3p392 :039 > t = w.tags
 => [#<Tag id: 32451, name: "c++", num_questions: 180854, created_at: "2013-02-25 07:55:55", updated_at: "2013-02-25 07:55:55">, #<Tag id: 32452, name: "performance", num_questions: 28543, created_at: "2013-02-25 07:55:55", updated_at: "2013-02-25 07:55:55">, #<Tag id: 32453, name: "optimization", num_questions: 10021, created_at: "2013-02-25 07:55:55", updated_at: "2013-02-25 07:55:55">, #<Tag id: 32454, name: "language-agnostic", num_questions: 5942, created_at: "2013-02-25 07:55:55", updated_at: "2013-02-25 07:55:55">, #<Tag id: 32455, name: "branch-prediction", num_questions: 28, created_at: "2013-02-25 07:55:55", updated_at: "2013-02-25 07:55:55">, #<Tag id: 32456, name: "books", num_questions: 2667, created_at: "2013-02-25 07:55:59", updated_at: "2013-02-25 07:55:59">, #<Tag id: 32457, name: "ebook", num_questions: 263, created_at: "2013-02-25 07:55:59", updated_at: "2013-02-25 07:55:59">, #<Tag id: 32458, name: "creative-commons", num_questions: 62, created_at: "2013-02-25 07:55:59", updated_at: "2013-02-25 07:55:59">, #<Tag id: 32459, name: "json", num_questions: 39746, created_at: "2013-02-25 07:56:03", updated_at: "2013-02-25 07:56:03">, #<Tag id: 32460, name: "content-type", num_questions: 874, created_at: "2013-02-25 07:56:03", updated_at: "2013-02-25 07:56:03">] 
1.9.3p392 :040 > t.clear
   (19.0ms)  BEGIN
   (85.2ms)  DELETE FROM "questions_tags" WHERE "questions_tags"."question_id" = 10053 AND "questions_tags"."tag_id" IN (32451, 32452, 32453, 32454, 32455, 32456, 32457, 32458, 32459, 32460)
   (36.5ms)  COMMIT
 => [] 
1.9.3p392 :041 > t
 => [] 
1.9.3p392 :042 > t
 => [] 
1.9.3p392 :043 > w.tags
 => [] 

2 Answers 2

3

The builtin method:

 your_array.clear
13
  • is that better than just doing t = []? Why or why not? Actually...just tried this and it seems that it actually deletes the record in the DB - not the records in the array...which is not what I wanted to do. Mar 20, 2013 at 9:20
  • Is deleting the records from the DB an expected behavior? For what it's worth, I am using Rails. Mar 20, 2013 at 9:23
  • I can't see how it would save your object Mar 20, 2013 at 9:28
  • I don't want to delete the records from the DB. I just want to empty the array. I am going to update the question with actual code and examples. Done....check update to question. Mar 20, 2013 at 9:29
  • then it's not an array, it's an active record relation. api.rubyonrails.org/classes/ActiveRecord/Associations/… Mar 20, 2013 at 9:34
0

Please take a look at the Rails doc on associations. The clear method is not the Array#clear, it's overriden by Rails.

I would just copy the elements in the association, this will ensure that the association will be lazy loaded, with something like:

tags = w.tags.collect{|t| t}
tags.clear

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.