如何在ruby ActiveRecord中销毁没有ID列的记录? [英] how can I destroy a record without an ID column in ruby ActiveRecord?

查看:69
本文介绍了如何在ruby ActiveRecord中销毁没有ID列的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有ID列的表。当我尝试使用ActiveRecord从中删除时,生成的SQL是 DELETE FROM table_name WHERE ID = NULL ,这显然不起作用。是否可以使用ActiveRecord从表中删除,或者至少使用占位符运行原始SQL删除查询(因此它不易受到SQL注入的攻击)?

I have a table without an ID column. When I try to delete from it using ActiveRecord the generated SQL is DELETE FROM table_name WHERE ID=NULL, which obviously doesn't work. Is there any way to delete from the table using ActiveRecord, or at least run a raw SQL delete query with placeholders (so it's not vulnerable to SQL injection)?

推荐答案

您是否尝试过使用ActiveRecord的delete_all方法?以下是我的沙箱中的摘录:

Have you tried using ActiveRecord's delete_all method? Here's an excerpt in a sandbox of mine:

>> customer = Customer.new(:login => 'foo')
=> #<Customer id: nil, status: 0, name: nil, login: "foo", password: nil, salt: nil, api_key: nil, address: nil, town: nil, state: nil, country: "USA", zipcode: nil, balance: #<BigDecimal:1032669b0,'0.0',9(18)>, discount: 0, last_four_cc: nil, auto_renew: false, contact_name: nil, contact_email: nil, domain: "anon.limelock.com", created_at: nil, updated_at: nil>
>> customer.save
=> true
>> Customer.all.count
=> 4
>> Customer.delete_all(:login => 'foo')
=> 1
>> Customer.all.count
=> 3

生成的SQL是从`customers` WHERE(`customers` .`login` ='foo')

查看文档

这篇关于如何在ruby ActiveRecord中销毁没有ID列的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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