Delete from database with specific details in ruby

扶醉桌前 提交于 2021-02-05 10:46:45

问题


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

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

ID in @recipient

I try this:

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

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

Error in this method :

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!


回答1:


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(','))

Here you will find the docs of older Rails versions.

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
)


来源:https://stackoverflow.com/questions/65324398/delete-from-database-with-specific-details-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!