The problem is your passing Ruby code as a String inside a where clause instead of using String interpolation.
I see what you're trying to do but I think you should let Rails handle the dates lookups for you.
You can simplify your code with the following:
class Invoice < ActiveRecord::Base
def self.thirtydays
where(invdate: 31.days.ago..DateTime.now)
end
end
31.days.ago..DateTime.now will generate a Range object starting with a date 31 days ago and ending with today's date. Rails will handle the SQL date range call for you depending on your database platform, which is Postgresql in this case.
It will generate a SQL statement similar to:
SELECT `invoices`.* FROM `invoices` WHERE (`invoices`.`invdate` BETWEEN '2013-09-24 22:00:00' AND '2013-10-25 22:00:00')