在某些情况下,default_scope会中断(update | delete | destroy)_all [英] default_scope breaks (update|delete|destroy)_all in some cases

查看:120
本文介绍了在某些情况下,default_scope会中断(update | delete | destroy)_all的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这是Rails 3中的错误.我希望这里的人能够引导我朝正确的方向发展.下面发布的代码仅用于说明此问题.希望这不会混淆这个问题.

I believe this is a bug in Rails 3. I am hoping someone here can steer me in the correct direction. The code posted below, is purely for illustration of this problem. Hopefully this does not confuse the issue.

鉴于我有一个Post模型和一个Comment模型.发表has_many条评论,评论发表到发表.

Given I have a Post model, and a Comment model. Post has_many Comments, and Comment belongs_to Post.

在Post模型上设置了default_scope,定义了joins()和where()关系.在这种情况下,where()依赖于joins().

With a default_scope set on the Post model, defining joins() and where() relations. In this case where() is dependent on joins().

通常,帖子将不依赖于评论.同样,我只想举一个简单的例子.当where()依赖于joins()时,可能是任何情况.

Normally Posts wouldn't be dependent on Comments. Again, I just want to give a simple example. This could be any case when where() is dependent on joins().

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:

Produces the following query, and ultimately throws 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.

Again, update_all, delete_all, destroy_all behave the same way. I discovered this behaviour when my application complained when trying to update the counter_cache. Which eventually drills down into update_all.

推荐答案

我也遇到了这个问题,但是我们确实需要能够在default_scope中具有复杂条件的情况下使用update_all(例如,没有默认设置)预先加载作用域是不可能的,将命名作用域直接粘贴到任何地方都根本不好玩).我在这里用修复程序打开了一个拉取请求:

I had this problem also, but we really needed to be able to use update_all with complex conditions in the default_scope (for example, without the default scope eager-loading is impossible, and pasting a named scope literally everywhere is no fun at all). I have opened a pull request here with my fix:

https://github.com/rails/rails/pull/8449

对于delete_all,如果存在连接条件以使您必须做的事情更加明显,那么我提出了一个错误(而不是仅仅抛出连接条件并在所有内容上运行delete_all,您都会收到错误).

For delete_all I've raised an error if there's a join condition to make it more obvious what you have to do (instead of just tossing the join condition and running the delete_all on everything, you get an error).

不知道Rails的人将如何处理我的请求,但认为这与本次讨论有关. (此外,如果您需要修复此错误,则可以尝试我的分支并在请求请求中发表评论.)

Not sure what the rails guys are going to do with my pull request, but thought it was relevant to this discussion. (Also, if you need this bug fixed, you could try out my branch and post a comment on the pull request.)

这篇关于在某些情况下,default_scope会中断(update | delete | destroy)_all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆