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]}


{:reg_token => params[:reg_token]}

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


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

Answer Source

I would just chain the where clauses:

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