# How to define static variable across a partial?

**URL:** <https://www.ruby-forum.com/t/how-to-define-static-variable-across-a-partial/103098>\
**Category:** Rails\
**Created:** [May 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/how-to-define-static-variable-across-a-partial/103098 "2007-05-01T20:55:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/how-to-define-static-variable-across-a-partial/103098/1 "2007-05-01T20:55:33Z")

</div>

Hi,

I have partial:  
\<%=render(:partial =\> ‘blog\_post’, :collection =\> @blogs)%\>

This outputs a series of blog posts, however I only want to output the  
created\_on date if it is different from the last, is there a way to  
compare these or create a static variable that I can assign the date to?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 1, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/how-to-define-static-variable-across-a-partial/103098/2 "2007-05-01T21:40:24Z")

</div>

josh wrote:

> Hi,
> 
> I have partial:  
> \<%=render(:partial =\> ‘blog\_post’, :collection =\> @blogs)%\>
> 
> This outputs a series of blog posts, however I only want to output the  
> created\_on date if it is different from the last, is there a way to  
> compare these or create a static variable that I can assign the date to?

You’re saying the @blogs array is already sorted by the created\_on date  
?

Then you can expand the loop

\<% last\_date = nil %\>

\<% @blogs.each do |blog| %\>  
\<%= render :partial =\> ‘blog\_post’, blog,  
:locals =\> { :ignore\_date =\> (last\_date == blog.created\_on)  
} %\>

\<% last\_date = blog.created\_on %\>

\<% end %\>

Then change the blog\_post partial to not show the created\_on date if  
variable “ignore\_date” has value true.

Stephan

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [May 1, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/how-to-define-static-variable-across-a-partial/103098/3 "2007-05-01T23:38:03Z")

</div>

I’m not sure if this is what you’re asking but this is what i’ve  
implemented in a partial called \_newest\_date.rhtml :

\<% if newest\_date.created\_at == newest\_date.updated\_at %\>  
\<%= newest\_date.created\_at.strftime(“Created on %m/%d/%Y”) %\>  
\<%= newest\_date.created\_at.strftime(“at %I:%M%p”) %\>  
\<% else %\>  
\<%= newest\_date.updated\_at.strftime(“Updated on %m/%d/%Y”) %\>  
\<%= newest\_date.updated\_at.strftime(“at %I:%M%p”) %\>  
\<% end %\>

And i call this for every post that is shown in a page. With a little  
more detail the list.rhtml file :

\<% for post in @posts %\>

### \<%= link\_to h(post.title), :action =\> 'edit', :id =\> post %\> ``` <ul class="post_info"> <%= render :partial => 'newest_date', :object => post %> </ul> [.... stuff here.....] ```

\<% end %\>

You could call a partial like this one from inside another partial.

I’m pretty sure this is NOT the best way to do it, but i’m pretty new  
at rails so this is a _fast_ solution i found for my problem, which in  
this case is to either print the created\_at date or the updated\_at  
date according to which one is the newest.

P.S.: The controller just returns the @posts collection through a  
paginator :

def list  
@post\_pages, @posts = paginate :posts, :order =\> ‘id  
DESC’, :per\_page =\> 10  
end

–Hope i helped a little
