我遇到了一個問題,我只需要從兩個活動記錄對象數組中刪除重複項。唯一的一點是它只是從數據庫中刪除它,我只需要在這種情況下從數組中刪除它。我跟着這個Remove object from an array of objects,也嘗試了一些其他的東西,他們能夠從內存中刪除它,並顯式地從數組中刪除它,而不是數據庫,但我無法複製它。任何建議都會很棒。謝謝大家!從數組中刪除對象而不從數據庫中刪除它
company_links = CompanyLinkType.where(company_id: company_ids, contact_linktype_id: 3)
other_company_links = CompanyLink.where(company_id: company_ids, link_type: 'Twitter')
company_links.each do |company_link|
other_company_links.each do |other_company_link|
# checks if id and url match, need to remove obj from company_link array
if other_company_link.company_id == company_link.company_id && other_company_link.url == company_link.contact_link_url
company_link.delete
Rails.logger.info"+++++++++DELETED++++++++++"
end
end
end
:'company_links || other_company_links',它會返回你需要的相同結果。 –