From 430c673fdff8c2e0df644d6cdfeda61a0bdde947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Thu, 18 Nov 2021 13:59:37 +0800 Subject: [PATCH] fix error --- app/models/site_feed.rb | 4 ++-- config/routes.rb | 4 ++++ lib/feed_model/cache.rb | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/site_feed.rb b/app/models/site_feed.rb index 4c93cc6..330b8c8 100644 --- a/app/models/site_feed.rb +++ b/app/models/site_feed.rb @@ -22,11 +22,11 @@ class SiteFeed end end before_destroy do - tmp = SiteFeedAnnc.where(feed_id: self.id).first + tmp = SiteFeedAnnc.where(feed_id: self.id) main_directory = File.join("#{Rails.root}","public","site_feeds") feed_directory = File.join(main_directory.to_s, self.id.to_s) FileUtils.rm_rf(feed_directory.to_s) - if !tmp.nil? + if tmp.count!=0 tmp.destroy end end diff --git a/config/routes.rb b/config/routes.rb index 0f544f8..2d5caee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,10 @@ Rails.application.routes.draw do end SiteFeedAnnc.where(:feed_id.nin=>SiteFeed.all.pluck(:id)).destroy SiteFeed.each do |site_feed| + count = SiteFeedAnnc.where(feed_id: site_feed.id).count + if count>1 + SiteFeedAnnc.where(feed_id: site_feed.id).limit(count-1).destroy + end tmp = SiteFeedAnnc.where(feed_id: site_feed.id).first if site_feed.disabled != true if tmp.nil? diff --git a/lib/feed_model/cache.rb b/lib/feed_model/cache.rb index 14520e2..84470f7 100644 --- a/lib/feed_model/cache.rb +++ b/lib/feed_model/cache.rb @@ -7,6 +7,10 @@ module FeedModel before_save :do_before_save end def recreate_annc_cache(feed) + count = SiteFeedAnnc.where(feed_id: feed.id).count + if count>1 + SiteFeedAnnc.where(feed_id: feed.id).limit(count-1).destroy + end tmp = SiteFeedAnnc.where(feed_id: feed.id).first if tmp.nil? tmp = SiteFeedAnnc.new(feed_id: feed.id)