mjpablo23 mjpablo23 -4 years ago 114
MySQL Question

How to delete row from Ruby Sequel table based on two conditions

I have a table (Device) that I am using Ruby to access. I want to delete a row if it fulfills these two conditions:

{:user_id => params[:user_id]}


and

{:reg_token => params[:reg_token]}


However, I'm not sure if I know how to use
AND
correctly. I am trying to follow this guide:

http://sequel.jeremyevans.net/rdoc/files/doc/dataset_filtering_rdoc.html

if Device.where{Sequel.&({:user_id => params[:user_id]}, {:reg_token => params[:reg_token]})}.count > 0
# device = Device.where((:user_id => params[:user_id]) & (:reg_token => params[:reg_token])).delete
device = Device.where{Sequel.&({:user_id => params[:user_id]}, {:reg_token => params[:reg_token]})}.delete
end

Answer Source

I would just chain the where clauses:

devices = Device.where(user_id: params[:user_id]).where(reg_token: params[:reg_token])
devices.delete_all
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download