I’m a bit dumfounded – getting an ‘undefined method `each’ for nil:NilClass’ error.
I’ve created a pretty straight-forward scope in the model
def self.just_added
where('created_at > ?', Time.now-7.days.ago).order("created_at desc").first(4)
end
And a loop in my view
<% @just_added_jobs.each do |job| %>
<%= link_to job.name, job %>
<% end %>
Not sure what’s going on – I’m fairly certian I’ve created a scope like before that worked fine.
Also – I also plan to do some more complex scoping with dates – I have several date fields as part of a model (i.e., to loop through the jobs that are due soon, past due). Where could I read more about scoping dates?
The result of the classmethod you show can never be
nilso we must conclude you’re failing to set@just_added_jobsin the controller.created_at > Time.now-7.days.agomakes no sense, you probably meancreated_at > 7.days.agoCalling
first(n)on a scope is a bad idea because the result is not a scope anymore but an array. You needlimit(n).