[英]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. 我使用delete_if编写了一个异常,遇到了问题,想使用SQL语句或使用活动记录的东西。
Here is my delete_if statement: 这是我的delete_if语句:
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. 基本上,该语句为如果所有用户location_ids都包含在location_ids格式中,则删除。
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! 注意:它实际上会破坏数据库中的记录!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.