1

I need help on how to delete records that exist in the DB but not in array sent in a request;

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

Below is my implementation: where am going wrong?

@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 

1 Answer 1

1

it's not clear to me where, in your code, you pick the ids sent in the array you showed before... so I'm assuming like this:

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

since you have your array like this, the only information you need to query on database is the ids (also, assuming the id's in the array are the id's on database, otherwise it wouldn't make sense). You can get them like this:

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

Also, it seems to me that, for the code you showed, you want to destroy them based on a specific course. There would be 2 ways to do that. First, using the relationship (I prefer like this one):

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

Or you can do the query directly on the Topic model, but passing the course_id param:

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

ActiveRecord is smart enough to mount that query correctly in both ways. Give it a test and see which works better for you

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Ronan-- Let me check and get back to you now
It has worked very well, However, it deletes new created records sent in the array. This is how i wanted it, for example I am adding a new topic to the DB it should not be deleted because its new and under new Topic i can send new subtopics as well and a user saves globally and if a user decides to delete topics under a course i should delete them because they wont appear in sent array for saving changes. And a user can also decide to delete subtopic under topic or many topics and save changes. The above answer works well. But now there a problem for deleting new records sent with it.
in which moment/step are you doing that delete? in a callback?
when i send an array i assume the ids have been deleted from frontend and are sent via API to update changes, if in the array sent Ids not exist and yet they are the DB i should delete the same. However, in the same array i can have new sent records to be saved and some others removed: I can have new topic or new subtopic details sent via api for making changes and a user can decide to remove one of the previous saved topics or subtopics on the course and i need to delete them as well: i dont want new records sent in the array under a topic to be deleted or under subtopic.
oooh, got it. In that case, when you send that to your backend, you should be able to identify which ones were removed. You can send a flag in the object like "removed": true. So, in your query, it would be something like: deleted_ids = objects_sent.select{|o| o['removed']}.map{|o| o['id'].to_i}. So you would only delete the correct ones. But your frontend should set that before send to backend
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.