I’m trying to loop through the most recent “Articles” – say, 10 most recently posted within last 30 days.
Should I be creating a method in my article model something like follows:
models/articles.rb
def recent
self.where('created_at > ?', Time.now-30.days.ago)
end
And then calling it in my views?
views
@articles.each do |article|
link_to article.title, Article.recent
end
This of course, does not work.
If this has been answered (maybe I was using wrong search terms – new to stackoverflow), direction is appreciated!
I think this should be in a method, but a class method. I personally prefer methods over scopes for this sort of thing.
In your controller, you can set
@recent_articleswithview you will then do
Recommended (dated) Reading: http://www.railway.at/2010/03/09/named-scopes-are-dead/