我相信这是Rails 3中的一个bug。我希望这里有人能够引导我朝着正确的方向前进。下面的代码,纯粹是为了说明这个问题。希望这不会混淆这个问题。default_scope break(update | delete | destroy)_all在某些情况下
鉴于我有一个Post模型和一个评论模型。发布has_many评论和评论belongs_to发布。
通过在Post模型上设置default_scope,定义joins()和where()关系。在这种情况下,()依赖于连接()。
通常帖子不会依赖于评论。再次,我只想举一个简单的例子。这可能是where()依赖于连接()的任何情况。
class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
default_scope joins(:comments).where("comments.id < 999")
end
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
end
运行以下命令:
Post.update_all(:title => Time.now)
产生如下的查询,并最终抛出的ActiveRecord :: StatementInvalid:
UPDATE `posts` SET `title` = '2010-10-15 15:59:27' WHERE (comments.id < 999)
再次,update_all,DELETE_ALL,destroy_all行为相同的方式。当我的应用程序在尝试更新counter_cache时发生抱怨时,我发现了这种行为。最终深入update_all。
嘿,本,你对此有疑问? – 2012-07-10 20:07:29