简体   繁体   English

消毒destroy_all的最佳方法是什么-Rails

[英]What's the best way to sanitize destroy_all - Rails

I have the following controller in Rails : 我在Rails具有以下控制器:

class FooController < ApplicationController
    def delete_foo(bar):
        Foo.destroy_all("foo = '#{@bar}'")

Is

Foo.destroy_all("foo = ?", @bar)

always valid? 一直有效吗?

destroy_all works on a relation. destroy_all处理关系。 Why not do 为什么不呢

 Foo.where(foo: bar).destroy_all

Foo.destroy_all("foo = ?", @bar) , This is invalid. Foo.destroy_all("foo = ?", @bar) ,这是无效的。

From apidoc , we will find: apidoc ,我们将找到:

destroy_all(conditions = nil) public

destroy_all method only accepts a single argument, the argument can be a string, array, or hash. destroy_all方法仅接受单个参数,该参数可以是字符串,数组或哈希。 You cannot pass two arguments. 您不能传递两个参数。

So you can write like this: 所以你可以这样写:

Foo.destroy_all("foo = #{@bar}")
Foo.destroy_all(foo: @bar)
Foo.where(foo: @bar).destroy_all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM