删除不在已发送数组中的现有记录 Rails 5 API

Delete existing Records if they are not in sent array Rails 5 API

我需要有关如何删除存在于数据库中但不存在于请求中发送的数组中的记录的帮助;

My Array:

[
    {   "id": "509",
        "name": "Motions move great",
        "body": "",
        "subtopics": [
            {   
                "title": "Tywan",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            },
            {
                
                "title": "Transportations Gracious",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            },
            {
                "title": "Transportation part",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            }
        ]
    },
    {
        "name": "Motions kkk",
        "body": "",
        "subtopics": [
            {   
                "title": "Transportations",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            }
        ]
    }
]

下面是我的实现:哪里出错了?

@topics = @course.topics.map{|m| m.id()}
                     @delete= @topics
                    puts @delete
                  if Topic.where.not('id IN(?)', @topics).any?
                      @topics.each do |topic|
                        topic.destroy
                    end 
                  end 

我不清楚在你的代码中,你在哪里选择了你之前显示的数组中发送的 id...所以我假设是这样的:

   objects_sent = [
    {   "id": "509",
        "name": "Motions move great",
        "body": "",
        "subtopics": [
            {   
                "title": "Tywan",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            },
            {
                
                "title": "Transportations Gracious",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            },
            {
                "title": "Transportation part",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            }
        ]
    },
    {
        "name": "Motions kkk",
        "body": "",
        "subtopics": [
            {   
                "title": "Transportations",
                "url_path": "https://ugonline.s3.amazonaws.com/resources/6ca0fd64-8214-4788-8967-b650722ac97f/WhatsApp+Audio+2021-09-24+at+13.57.34.mpeg"
            }
        ]
    }
]

既然你有这样的数组,你需要在数据库上查询的唯一信息就是id(另外,假设数组中的id是数据库中的id,否则它就没有意义)。你可以这样得到它们:

sent_ids = objects_sent.map{|o| o['id'].to_i}

另外,在我看来,对于您展示的代码,您想根据特定的过程销毁它们。有两种方法可以做到这一点。首先,使用关系(我更喜欢这样):

@course.topics.where.not(id: sent_ids).destroy_all

或者您可以直接在主题模型上进行查询,但传递 course_id 参数:

Topic.where(course_id: @course.id).where.not(id: sent_ids).destroy_all

ActiveRecord 足够聪明,能够以两种方式正确挂载该查询。测试一下,看看哪个更适合您