【问题标题】:usage of find_by_sql to delete record使用 find_by_sql 删除记录
【发布时间】:2009-04-24 00:00:39
【问题描述】:

我正在尝试从表“abc”中删除几条记录,其中 id 是从“xyz”表中获取的

SQL:

delete from abc where id in (select ids from xyz where i= 10)

我想使用 find_by_sql 执行此查询以避免多次命中数据库。

它给出了错误。请帮助我在对 DB 的一次点击中完成此删除操作。

【问题讨论】:

  • 这是错误:当你没有预料到它时,你有一个 nil 对象!评估类中的 nil.all_hashes 时发生错误:

标签: ruby-on-rails


【解决方案1】:

我不认为find_by_sql 是你想要的方法。

如果你有

class Abc < ActiveRecord::Base
end

(所以 abc.rb 是你的模型)那么我认为你应该能够做这样的事情:

Abc.connection.execute('delete from abc where id in (select ids from xyz where i= 10)')

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多