Sarah
Sarah

Reputation: 516

Deleting top N records in Rails 4

I can't believe I can't find how to do this, but I'm having trouble deleting the top 20 records in Rails 4.

I'm trying variations of this:

Comment.all(:order=> 'id asc', :limit => 20).delete

or

Comment.find(:all, :order => 'id asc', :limit => 20).delete

I'm getting this error:

ArgumentError: wrong number of arguments (1 for 0)

What am I doing wrong?

Upvotes: 0

Views: 79

Answers (1)

Richard Hamilton
Richard Hamilton

Reputation: 26434

You need to use destroy_all

Comment.order('id').limit(20).destroy_all

Upvotes: 1

Related Questions