简体   繁体   中英

Rails convert delete_if to sql statement

I have written an exception using delete_if and I am running into issues and would like to use an SQL statement or something using active record.

Here is my delete_if statement:

Format.all.delete_if { |f| ( f.location_ids.sort & User.current.location_ids.sort ) == User.current.location_ids.sort }

Basically, the statement is If all user location_ids are included in format location_ids then delete.

I don't fully understand your question, but maybe this code is what you're looking for:

current_user_location_ids = User.current.location_ids.sort

formats_to_destroy = Format.all.to_a.select do |format|
  format.location_ids.sort == current_user_location_ids
end

formats_to_destroy.map(&:destroy)

ATTENTION: It will actually destroy the record(s) in the DB!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM