从数据库中删除具有 ruby​​ 中的特定详细信息 [英] Delete from database with specific details in ruby

查看:37
本文介绍了从数据库中删除具有 ruby​​ 中的特定详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从数据库中删除所有行的 id 包含在某个数组或 (key,value) 中

I try to delete from database all row has a id include in some array or (key,value)

"recp" => "1, 2, 3 , 6, 7 , ........."

@recipient

我试试这个:

 @v = NameOfDatabase.where.not(:id=> @recipient.split(',').map(&:to_i), :conditions => {:thread =>@dis_PI.m_id}).destroy_all

在特定条件下,我想删除具有此条件的行,并且不包含在 @recipient

With specific condition i want to remove row with this condition and not include in @recipient

此方法中的错误:

NoMethodError (undefined method `where' for #<Class:0x7f447f57b140>):

我尝试了多个代码但没有工作,我多次提出这个问题但仍然没有工作!

I try multiple code but not working, i put this question multiple time but also not work yet!

推荐答案

从评论中,我了解到您正在运行一个非常旧的 Ruby on Rails 版本——可能已经有 10 多年的历史了.在 Rails 3.0 中,finder 方法完全改变了,因此所有当前的 Rails 文档将不再有用.特别是 where 方法在 Rails 3.0 之前是不存在的

From the comments, I learned that you are running a very old version of Ruby on Rails – probably more than 10 years old. With Rails 3.0 the finder methods change completely and therefore all current documentation for Rails will not be helpful anymore. Especially the where method did not exist before Rails 3.0

在如此旧的版本中,以下应该可以工作:

In such an old version the following should work:

YourModel.destroy_all("id in (?)", @recipient.split(','))

您可以在此处找到旧 Rails 版本的文档.

Here you will find the docs of older Rails versions.

条件基本上只是一个SQL片段.当您想添加更多条件时,您需要将所有条件写在一行中,如下所示:

The condition is basically just one SQL fragment. When you want to add more conditions then you need to write all conditions in one line like this:

YourModel.destroy_all(
  "id IN (?) AND thread = ?", @recipient.split(','), @dis_PI.m_id
)

这篇关于从数据库中删除具有 ruby​​ 中的特定详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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