0

Thanks for taking a look at this relatively nu-b question.

I have a web app built on Rails 3 that allows users to view multiple stories at a time, with each story having multiple posts. I use JS to poll the server at regular intervals so to search for new posts on all of the open stories. I use session variables so to keep track of where I ended my last search for each of those open stories so that I don't have to search the entire table of posts from scratch each time I poll the server.

Here is the action for when a user first opens a story:

def open_story
    story = Story.find(params[:story_id])
    #keep track of the last post searched for each open story so to assist when we poll for new posts to that story
    last_post_searched = Post.last.id
    session["last_post_searched_for_story_#{story.id}"] = last_post_searched 
    @posts = story.posts.where("posts.id <= ?", last_post_searched)
    respond_with @posts
end

Here is the action for when the client polls the server for new post updates on an array of open stories:

  def update_stories
    open_stories_id_array = params[:open_stories]
    open_stories_id_array.each { |open_story_id|
debugger
      start_search_at_post_id = session["last_post_searched_for_story_#{open_story_id}"] + 1
      session["last_post_searched_for_story_#{open_story_id}"] = Post.last.id
      story = Story.find(open_story_id)
      updates = story.posts.where("posts.id between ? AND ?", 
          start_search_at_post_id, session["last_post_searched_for_story_#{open_story_id}"])
      results[open_story_id] = updates
    }
    respond_with(results)
  end

For reasons that I can't figure out, my session variables don't increment to the new Post.last.id in my update_stories action in a timely fashion. Here is how I can recreate the problem:

  1. Say I have 30 posts in my db to various different stories.
  2. I call open_story on story 1. This sets session["last_post_searched_for_story_1"] to 30.
  3. I then make a new post to story 1 (post 31).
  4. My client polls the update_stories action to get new posts for story 1.
  5. update_stories searches for posts with ids between 31 and 31 for story with id of 1, and returns the post that I just made.
  6. Then, a little while later my client automatically polls update_stories again to check for any new posts on story 1. This is where the problem occurs.

Instead of session["last_post_searched_for_story_1"] containing the value, 31, it retains its previous value of 30 so that my db search returns my original new post for a second time. Often, my client will call update_stories several times before session["last_post_searched_for_story_1"] increments to 31. It's almost as if the session variable is very slow to save its new value, or I'm experiencing some sort of lazy loading problem.

Any help figuring this problem out would be greatly appreciated and eagerly accepted.

Thanks

BTW, as I still have a lot to learn, feel free to give feedback on better ways to handle this issue or if I am violating any rails best practices.

4

1 回答 1

-1

我发现您的代码存在 2 个问题:

您可能希望在应用最后一种方法之前先对结果进行排序。数据库返回的最后一条记录不一定是要创建的最后一条。
其次,要选择最后一个帖子,您应该仅将最后一个标准应用于该故事的帖子,然后选择该故事的最后一个帖子。
所以,而不是这个:

story = Story.find(params[:story_id])
#keep track of the last post searched for each open story so to assist when we poll for new posts to that story
last_post_searched = Post.last.id

你可以这样:

story = Story.find(params[:story_id])
last_post_searched = Post.joins(:stories).where("stories.id = ?", story.id).order("posts.created_on DESC").first  
于 2012-05-23T23:38:53.493 回答