# Showing the items visited for a non-logedin users

**URL:** <https://www.ruby-forum.com/t/showing-the-items-visited-for-a-non-logedin-users/180793>\
**Category:** Rails\
**Created:** [December 29, 2009, 9:03am UTC](https://www.ruby-forum.com/t/showing-the-items-visited-for-a-non-logedin-users/180793 "2009-12-29T09:03:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wilhelm\_Longshanks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wilhelm\_Longshanks](https://www.ruby-forum.com/u/Wilhelm_Longshanks)\
**Post date:** [December 29, 2009, 9:03am UTC](https://www.ruby-forum.com/t/showing-the-items-visited-for-a-non-logedin-users/180793/1 "2009-12-29T09:03:58Z")

</div>

Hi - I would like to implement a feature similar to “Items you have  
visited” in Amazon.  
The requirement is that the feature is also available for those who  
are not logged-in.

I have a User and Item models. I use Rails-default session storage.  
I do not need when exactly each item page is visited, but I need to  
keep the order of them  
so that I can show the most recently visited item on top of the list.

Do I need to create and save every user and save the history of  
visited items in the database?

- Wil

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Wilhelm\_Longshanks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wilhelm\_Longshanks](https://www.ruby-forum.com/u/Wilhelm_Longshanks)\
**Post date:** [December 29, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/showing-the-items-visited-for-a-non-logedin-users/180793/2 "2009-12-29T19:14:31Z")

</div>

Wilhelm Longshanks wrote:

> Hi - I would like to implement a feature similar to “Items you have  
> visited” in Amazon.  
> The requirement is that the feature is also available for those who  
> are not logged-in.
> 
> I have a User and Item models. I use Rails-default session storage.  
> I do not need when exactly each item page is visited, but I need to  
> keep the order of them  
> so that I can show the most recently visited item on top of the list.
> 
> Do I need to create and save every user and save the history of  
> visited items in the database?

No. If you just save the ids of the last viewed items in an array and  
keep this array short there should be no problem with the cookie-based  
sessions.

Just do something like:

session[:last\_viewed] ||= []  
session[:last\_viewed] =  
session[:last\_viewed].unshift(@item.id).uniq[0,5]

With this you have the ids of the last (max. 5) @item(s) in  
session[:last\_viewed] in descending order.

Hope this helps, T.
