从表中删除与数组中的数据匹配的记录?

发布于 2024-08-26 04:50:37 字数 141 浏览 3 评论 0原文

我有一个包含 2 个字段的表。单词和时间戳。 然后我有这个包含一些单词的数组。 如何删除表中与数组中的单词匹配的所有记录? 假设该模型称为“Word”。

关于如何实现这一目标有什么想法吗?也许循环遍历数组并运行一些销毁查询。有人可以指导我到这里吗?谢谢

I have a table of 2 fields. Word and timestamp.
Then i have this array which contains some words.
How do i delete all the records in the table which match with the words in the array?
Suppose that the model is called "Word".

Any ideas on how to achieve this? maybe loop through the array and run some destroy queries. Can anybody direct me here? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2024-09-02 04:50:37

执行此操作:

Word.delete_all(:words => words_array)

这将在一个 SQL 语句中删除与给定数组中的单词匹配的行。

例如:

words = ["pop", "pop alternative", "r&b"]
Word.delete_all(:words => words)

Do this:

Word.delete_all(:words => words_array)

This will delete the rows matching the words in the given array, in ONE SQL statement.

E.g.:

words = ["pop", "pop alternative", "r&b"]
Word.delete_all(:words => words)
不知在何时 2024-09-02 04:50:37

如果您在模型上定义了回调,则裸 SQL 将不会调用它们。这种情况下推荐的方式是:

deletable_words = [ 'php', 'c++' ]
objs = Word.find(:all, :conditions => [ "words.word IN (?)",  deletable_words])
objs.each { |o| o.destroy }

If you defined callbacks on the model bare sql won't call them. The recommended way in this case is:

deletable_words = [ 'php', 'c++' ]
objs = Word.find(:all, :conditions => [ "words.word IN (?)",  deletable_words])
objs.each { |o| o.destroy }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文