# Trying to delete all but most recent 10

**URL:** <https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046>\
**Category:** Rails\
**Created:** [September 6, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046 "2009-09-06T23:40:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 6, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/1 "2009-09-06T23:40:46Z")

</div>

I have a table where I am trying to delete all but the 10 most recent  
records. My code is as follows:

cnotes = Channelnote.find(:all, :order =\> ‘tstamp DESC’, :limit =\>  
10, :conditions =\> [“deleted=0”])  
Channelnote.delete\_all;  
for cnote in cnotes do  
g=cnote.save  
end

However, it deletes ALL records in the table, seeming to NOT write  
cnotes back. Can anyone please explain to me why/ Myu log for this is  
enclosed below:

Processing AssociateSessionsController#destroy (for 127.0.0.1 at  
2009-09-06 17:11:07) [GET]  
Parameters: {“action”=\>“destroy”,  
“controller”=\>“associate\_sessions”}  
[4;35;1mAssociate Columns (16.0ms) [0m [0mSHOW FIELDS FROM  
`associates` [0m  
[4;36;1mAssociate Load (0.0ms) [0m [0;1mSELECT \* FROM  
`associates` WHERE (`associates`.`id` = ‘1’) LIMIT 1 [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mAssociate Update (0.0ms) [0m [0;1mUPDATE `associates`  
SET `perishable_token` = ‘Tz-demkWdZreygqeTVrd’, `updated_at` =  
‘2009-09-06 21:11:07’, `last_request_at` = ‘2009-09-06 21:11:07’ WHERE  
`id` = 1 [0m  
[4;35;1mSQL (16.0ms) [0m [0mCOMMIT [0m  
[4;36;1mChannelnote Load (0.0ms) [0m [0;1mSELECT \* FROM  
`channelnotes` WHERE (deleted=0) ORDER BY tstamp DESC LIMIT 10 [0m  
[4;35;1mChannelnote Delete all (16.0ms) [0m [0mDELETE FROM  
`channelnotes` [0m  
[4;36;1mChannelnote Columns (0.0ms) [0m [0;1mSHOW FIELDS FROM  
`channelnotes` [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (15.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mSQL (0.0ms) [0m [0mBEGIN [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
Redirected to [http://localhost:3000/](http://localhost:3000/)  
Completed in 406ms (DB: 63) | 302 Found [[http://localhost/logout](http://localhost/logout)]  
[4;35;1mSQL (0.0ms) [0m [0mSET SQL\_AUTO\_IS\_NULL=0 [0m

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 12:20am UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/2 "2009-09-07T00:20:00Z")

</div>

On Sun, Sep 6, 2009 at 2:39 PM, RVince[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, it deletes ALL records in the table, seeming to NOT write  
> cnotes back.

More idiomatically –

@cnotes = Channelnote.find(:all, :order =\> ‘tstamp DESC’,  
:limit =\> 10, :conditions =\> [“deleted=0”])  
Channelnote.delete\_all;  
@cnotes.each { |cnote| cnote.save! }

The save! will let you know why the save failed (failing validation,  
or…)

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 2:23am UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/3 "2009-09-07T02:23:29Z")

</div>

Hassan,

Same problem though – it doesn;t save them back. They just all get  
deleted, nothing saved back. Here is my code (all the saves evaluate  
to true, btw) and the log:

puts “deleting all but 10 channelnotes”  
@cnotes = Channelnote.find(:all, :order =\> ‘tstamp DESC’, :limit =\>  
10, :conditions =\> [“deleted=0”])  
Channelnote.delete\_all;  
@cnotes.each { |cnote|  
s = cnote.save!  
puts s  
}

Processing AssociateSessionsController#destroy (for 127.0.0.1 at  
2009-09-06 20:08:09) [GET]  
Parameters: {“action”=\>“destroy”,  
“controller”=\>“associate\_sessions”}  
[4;36;1mAssociate Columns (16.0ms) [0m [0;1mSHOW FIELDS FROM  
`associates` [0m  
[4;35;1mAssociate Load (0.0ms) [0m [0mSELECT \* FROM `associates`  
WHERE (`associates`.`id` = ‘1’) LIMIT 1 [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mAssociate Update (0.0ms) [0m [0mUPDATE `associates` SET  
`perishable_token` = ‘WF\_FpyItpFvRCghqAd5W’, `updated_at` =  
‘2009-09-07 00:08:09’, `last_request_at` = ‘2009-09-07 00:08:09’ WHERE  
`id` = 1 [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mCOMMIT [0m  
[4;35;1mChannelnote Load (0.0ms) [0m [0mSELECT \* FROM  
`channelnotes` WHERE (deleted=0) ORDER BY tstamp DESC LIMIT 10 [0m  
[4;36;1mChannelnote Delete all (0.0ms) [0m [0;1mDELETE FROM  
`channelnotes` [0m  
[4;35;1mChannelnote Columns (0.0ms) [0m [0mSHOW FIELDS FROM  
`channelnotes` [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (16.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
[4;36;1mSQL (0.0ms) [0m [0;1mBEGIN [0m  
[4;35;1mSQL (0.0ms) [0m [0mCOMMIT [0m  
Redirected to [http://localhost:3000/](http://localhost:3000/)  
Completed in 282ms (DB: 32) | 302 Found [[http://localhost/logout](http://localhost/logout)]

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 5:21am UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/4 "2009-09-07T05:21:57Z")

</div>

On Sun, Sep 6, 2009 at 5:12 PM, RVince[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Same problem though – it doesn;t save them back. They just all get  
> deleted, nothing saved back. Here is my code (all the saves evaluate  
> to true, btw) and the log:

If the save! returns true, it should be good – what DB are using?  
and what version of Rails and db adapter?

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/5 "2009-09-07T13:23:42Z")

</div>

Using MySQL 5.0.27 Community Edition, Rails 2.3.2 and MySQL adapter.

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 7:02am UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/6 "2009-09-07T07:02:36Z")

</div>

Not sure why (or if) save is returning true, but looking at the  
program flow, wouldn’t Rails try to do an update of each cnote since  
they are not new records when they are retrieved? (Rather than an  
insert as the OP intends.)

In that case, trying to update a record that no longer exists in the  
database should return false but perhaps it doesn’t in this case.

On Sep 6, 6:23 pm, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/7 "2009-09-07T14:01:45Z")

</div>

On Sep 7, 1:12 am, RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hassan,
> 
> Same problem though – it doesn;t save them back. They just all get  
> deleted, nothing saved back. Here is my code (all the saves evaluate  
> to true, btw) and the log:

As of 2.1 rails has support for partial updates - when you do a save  
only changed columns are written back. If there were no changes at all  
(as in your case) then the save is a no-op. The change tracking  
mechanism doesn’t know that you’ve deleted a bunch of rows. There are  
methods for saying ‘i’ve changed this attribute’ (foo\_will\_change!)  
but there doesn’t seem to be one for “I’ve changed all the attributes,  
just save the whole record”, although it shouldn’t be hard to write  
one. You could also play around with turning off partial updates  
(perhaps only for the duration of the relevant bit of code)

Fred

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 7:55pm UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/8 "2009-09-07T19:55:55Z")

</div>

Ah, thanks Fred,

Yes, this explains then why my other records are not being saved back  
– I get it (though, this has nothing to do with the partials –  
that’s a different issue. In this particular issue, I am merely  
pruning the table when the user logs off). Thanks! -RVince

On Sep 7, 8:00 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [September 7, 2009, 8:12pm UTC](https://www.ruby-forum.com/t/trying-to-delete-all-but-most-recent-10/175046/9 "2009-09-07T20:12:10Z")

</div>

RVince wrote:

> I have a table where I am trying to delete all but the 10 most recent  
> records. My code is as follows:
> 
> cnotes = Channelnote.find(:all, :order =\> ‘tstamp DESC’, :limit =\>  
> 10, :conditions =\> [“deleted=0”])  
> Channelnote.delete\_all;  
> for cnote in cnotes do  
> g=cnote.save  
> end

This is terrible logic. In another thread, I gave you a sample SQL  
query for only deleting the records you actually want deleted. I  
strongly suggest that you do it that way instead of deleting everything  
and then writing stuff back in.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
