I can not appear to remove old records from the application on heroku.

Presently the heroku scheduler runs:

class Post < ActiveRecord::Base

def self.get_data
  # this populates my app but the database is starting to get large and I don't need the old records
  scr = Scrape.new
  data_array = scr.scrape
  # destroy_old_data

but Let me uncomment the "destory_old_data" call.

def destroy_old_data
  # oldest = Post.where("updated_at > ?", 30.days.ago)
  # Post.delete_all("updated_at > ?", 30.days.ago)
  # Post.destroy_all("updated_at > ?", 30.days.ago)
  oldest = Post.find(:all, "updated_at > ?", 30.days.ago)

As you can tell, I have attempted a couple of stuff that appear to operate in your area within the console however i can't encourage them to focus on heroku.

The errors I am getting are just like:

ArgumentError: wrong number of arguments (2 for 1)


when using Post.find
NoMethodError: undefined method `destroy' for #<Array:0x00000004579898>

What's the simplest call I'm able to make to locate and take away each one of these old records effectively?

Any assistance could be appreciated.


To deal with the NoMethodError, you have to iterate with the variety of records and call destroy on each one of these.

oldest.each { |r| r.destroy }

or you might make use of a method like destroy_all in which you don't even have to do an additional find. http://apidock.com/rails/ActiveRecord/Base/destroy_all/class

Post.destroy_all(['updated_at > ?', 30.days.ago])

Putting the circumstances within an assortment should solve the ArgumentError as @luke-chadwick pointed out in the answer.

One item to think about with this particular is the number of records you're removing (here is a note in the paperwork):

Note: Instantiation, callback execution, and deletion of every record could be time intensive when you are getting rid of many records at the same time. It creates a minumum of one SQL Remove query per record (or even more, to enforce your callbacks). If you wish to remove many rows rapidly, without concern for his or her associations or callbacks, use remove_all rather.

delete_all might be a quicker choice for you unless of course you have to cleanup other connected records too, like Comments.

You are attempting a "remove" with an array, a listing of posts. You need to call a remove on the items for the reason that list rather.

Post.where(["updated_at > ?", 30.days.ago]).each do |post|

Where, takes one argument. And returns a listing of Posts that match its criteria. After that you can loop of these posts and perform an action (delete) on each.

The Post.destroy_all method takes anything that may be passed towards the conditions parameter of find(:all). Including a string, hash or perhaps an array.

Should you show the origin within the documentation for destroy whatever you should check this out:

# File activerecord/lib/active_record/base.rb, line 879
def destroy_all(conditions = nil)
  find(:all, :conditions => conditions).each { |object| object.destroy }

As you can tell, it requires just one argument. Therefore the conditions have to be covered with array braces (as proven):

Post.destroy_all(['updated_at > ?', 30.days.ago])