1

I'm working on a Ruby/Rails application in which I only want a user to be able to have one post seen at a time by all other users. All users can see all posts by other users, but only one post. What would be the simplest way of doing this?

This would be on a feed shared by all users.

The goal is that no user would have more than one post on the main feed. So when they post a new post it would remove the previous post from the shared feed.

  • Do you mean on the user's `show` page or in a feed? Either way you just filter by time created and then display only one. – Alex Lynham Apr 16 '13 at 17:58
  • Effectively you may potentially have something like this `<%= post.author %> said <%= time_ago_in_words(post.created_at) %> ago` so that if a post was submitted it would then say `Douglas said 3 minutes ago` – Deej Apr 16 '13 at 17:59

2 Answers2

0

Seems like you'd have to have some sort of simple "checkout" system for a post. When any user views a certain post, it automatically "checks out" that post and no one else can see it. The tricky part, it seems to me, is checking that post back in. How do you know when someone is done looking at a post? One fairly sure way is if the user navigates to a different page, but that's not the only way, and just because someone navigates to a new page doesn't mean they didn't leave the original post open in a different tab. And what if the user viewing the post just gets up and walks away from the computer for the day, with the post still up? Or what if they leave the post open, but move onto other browsing activities?

It seems like you might need to have a way for each checkout to "expire" after a certain period of time, and maybe the user viewing the post can "renew" the post when their checkout period is about to expire.

Then, obviously, you let a user view a particular post only if it's not already checked out by someone else.

I'm happy to supply ideas regarding the nuts and bolts of how to implement such a system, but I'll save that for later in case someone else has a better conceptual idea, or in case I totally misunderstood your question.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
0

You could add a post_id and post_viewed_at attribute to the user record, and try to keep that up to date.

But there are a couple of corner cases where this might cause problems:

  • when the user closes the browser window
  • when the user navigates away with the back button or by typing another URL

You'll need some JavaScript to try to make sure that the post_id and post_viewed_at attributes are zeroed out when the user navigates away or closes the window.

Using the post_viewed_at timestamp, you could also implement an expiry time in case there are scenarios where you did not catch the user navigating away.

Another problem is that this could cause quite a lot of database transactions for a site with many users.

Tilo
  • 33,354
  • 5
  • 79
  • 106