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)