> And, of course, that race condition exists in your console experiment, 
> but that's not an issue in the controller method I presented earlier. 

Yes, it is.  What happens when two people hit the URL that triggers that action 
at the same time?

Change the action to this...

def show
 @poem = Poem.find(params[:id])
 sleep(rand(10)) # to simulate something that takes awhile
 @poem.increment!(:view_count)
 ...
 ...
end

Let's say that view_count is 1 for the given poem.  Hit that page with two 
browsers at the same time and when they both finish view_count will be 2.  It 
should be 3.

> The @poem instance would be garbage collected at the end of the 
> request-response cycle.

True, but even quick Rails processes take a little bit of time.  As soon as 
there is *any* time between when you retrieve the record and when it gets 
incremented, you have a chance for a race condition because the increment (via 
increment!()) isn't incrementing.  It's overwriting.  

> Other methods would have to reload from the 
> database anyway. So I don't see a problem using the increment! method 
> here.
> 
> ------------------------------
> def show
>  @poem = Poem.find(params[:id])
>  @poem.increment!(:view_count)
>  ...
>  ...
> end
> ------------------------------
> -- 
> Posted via http://www.ruby-forum.com/.
> 
> -- 
> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
> 

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to