2015-05-14 31 views

回答

3

destroy_all適用於關係。爲什麼不做

Foo.where(foo: bar).destroy_all 
1

Foo.destroy_all("foo = ?", @bar),這是無效的。

apidoc,我們會發現:

destroy_all(conditions = nil) public 

destroy_all方法僅接受一個參數,該參數可以是一個字符串,數組或散列。你不能傳遞兩個參數。

所以你可以這樣寫:

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

您可以承受的'SQL'攻擊那裏。 – cybertextron

+0

是的,我們應該考慮在SQL中使用外部字符串時的安全後果。 – pangpang

相關問題