How about something like this:
<% if job.published_at > 5.days.ago %>   #credit to Colin for catching this
 <%= render :partial => "job" %>
<% else %>
 <% cache job %>
   <%= render :partial => "job" %>
 <% end %>
<% end %>

# _job.html.erb
   <p class="title"><%= job.title %></p>
   <p class="location"><%= job.location %></p>
   <p><%= job.published_at %></p>


I'd also look at turning the logic on that first line into a method

def recently_published?
  job.published_at > 5.days.ago
end

Luke

On 2010-10-19, at 2:00 PM, Greg Ma wrote:

> Hi,
> 
> I want to cache objects only if they were published at least 5 days ago.
> He is the code I've done, but I don't like because it is not dry...
> How could I improve it?
> 
> <% if job.published_at < 5.days.from_now %>
>  <p class="title"><%= job.title %></p>
>  <p class="location"><%= job.location%></p>
>  <p><%= distance_of_time_in_words(DateTime.now, job.published_at)
> %></p>
> <% else %>
>  <% cache job %>
>    <p class="title"><%= job.title %></p>
>    <p class="location"><%= job.location %></p>
>    <p><%= job.published_at %></p>
>  <% end %>
> <% end %>
> 
> Greg
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to