Showing the items visited for a non-logedin users

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

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.