Rails 3 -increment_counter 增加两次
当我运行这行代码时
Movie.increment_counter :views, @moive.id
,视图列将增加两次(+2,而不是+1)。在终端中,我看到这个对数据库运行的查询:
UPDATE `movies` SET `views` = COALESCE(`views`, 0) + 1 WHERE `movies`.`id` = 8
如果我尝试直接向 MySQL 运行这个查询,则 views 的值会正确递增一次 (+1)。
有什么提示吗,我错过了什么或者我还没有设置什么?
When I run this line of code
Movie.increment_counter :views, @moive.id
So the column views will be incremented twice (+2, not +1). In terminal I see this ran query to database:
UPDATE `movies` SET `views` = COALESCE(`views`, 0) + 1 WHERE `movies`.`id` = 8
If I attempt to run this query direct to MySQL, co the value of views is incremented correctly once (+1).
Any tips, what am I missing or I haven't set up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有机会跟踪页面浏览量?我也遇到了这个问题 - 对于每次页面加载,我都会看到页面视图计数器增加 3,而不是 1,但仅在生产中。
原来是Google的Adsense代码远程加载页面。我注意到,每当我的一个用户点击该页面,他们就会点击该页面两次,从而有效地产生了 3 次页面浏览。我怀疑他们这样做是为了验证页面上的内容是否符合他们的准则,并帮助将广告与页面内容适当匹配。检查您的 httpd 日志中是否有
Mediapartners-Google
。我敢打赌这就是正在发生的事情。一般建议:使用 Google Analytics(或类似服务)来跟踪页面浏览量。就我而言,我仍然需要在数据库中跟踪它,因为我根据某些页面操作的“受欢迎程度”实现自动完成,但您可能不需要这个。
Are you tracking page views by any chance? I ran into this as well- for every page load I would see the page view counter increment by three rather than 1, but only in production.
It turned out to be Google's Adsense code loading the page remotely. I noticed that they hit the page twice for every time one of my users would hit the page, effectively resulting in 3 page views. I suspect they do this to verify that content on the page meets their guidelines and to help match ads appropriately to page content. Check your httpd logs for
Mediapartners-Google
. I bet that's what's going on.General advice: use Google Analytics (or similar service) for tracking page views. In my case I still needed to track this in a DB because I implement autocomplete based on "popularity" of certain page actions, but you might not need this.