I have been trying various ways of how to display a list of registration in this case of companies for years, at the moment I have managed to display the companies created in this year, by means of:
def self.year
where(created_at: Time.zone.now.beginning_of_year..Time.zone.now.end_of_year)
end
But I can't get it to show me the records less than the current year, I tried to create it this way, but it doesn't print anything:
def self.before_year
where(created_at: Time.now - 1.year)
end
I hope you can give me a suggestion friends, greetings!
It is indicating an exact date. You're saying to return records that were created exactly one year ago.
Following the example above, in which you gave a range, you could do something like this:
However, this is not very elegant (why 1000 years?). Personally, I prefer to write SQL directly in this case:
That said, instead of a class method, you'd probably prefer to define a scope: