Commit f8efe66627d591159cd8fec5e2134a6218c2c689
1 parent
f932e31b
Exists in
master
and in
22 other branches
ActionItem948: bypass ActiveRecord on hit
This way (I hope) the non-deterministic problem of StaleObjectError stops happening.
Showing
2 changed files
with
3 additions
and
1 deletions
Show diff stats
app/models/article.rb
@@ -225,7 +225,8 @@ class Article < ActiveRecord::Base | @@ -225,7 +225,8 @@ class Article < ActiveRecord::Base | ||
225 | end | 225 | end |
226 | 226 | ||
227 | def hit | 227 | def hit |
228 | - self.increment!(:hits) | 228 | + self.class.connection.execute('update articles set hits = hits + 1 where id = %d' % self.id.to_i) |
229 | + self.hits += 1 | ||
229 | end | 230 | end |
230 | 231 | ||
231 | def can_display_hits? | 232 | def can_display_hits? |
test/unit/article_test.rb
@@ -610,6 +610,7 @@ class ArticleTest < Test::Unit::TestCase | @@ -610,6 +610,7 @@ class ArticleTest < Test::Unit::TestCase | ||
610 | should 'increment hit counter when hitted' do | 610 | should 'increment hit counter when hitted' do |
611 | a = Article.create!(:name => 'Test article', :profile => profile, :hits => 10) | 611 | a = Article.create!(:name => 'Test article', :profile => profile, :hits => 10) |
612 | a.hit | 612 | a.hit |
613 | + assert_equal 11, a.hits | ||
613 | a.reload | 614 | a.reload |
614 | assert_equal 11, a.hits | 615 | assert_equal 11, a.hits |
615 | end | 616 | end |