1

这是原始逻辑

(scrape_datas = ScrapeData.find(
  :all, :conditions => 
  "artist_status = 'NOT_FOUND' 
   AND blacklisted = 1 
   AND extracted = 0 
   and not EXISTS(
     SELECT * FROM artist_name_suggestions where original = artist_name
   )

我已经能够更好地分割第一部分

scrape_datas = ScrapeData.where(
  :artist_status => 'NOT_FOUND',
  :blacklisted   => 1,
  :extracted     => 0
)

尽管在将“而不是 EXISTS”查询混入其中时遇到问题

and not EXISTS(
  SELECT * FROM artist_name_suggestions where original = artist_name
)

谢谢!

4

1 回答 1

1

首先,您可以提取简单的范围:

scope :not_found, where(:artist_status => 'NOT_FOUND')
scope :blacklisted, where(:blacklisted => 1)
scope :extracted, where(:extracted => 0)

然后添加一个查询方法(假设artist_name是scrape_datas的一列):

def self.no_suggestions
  scrape_datas = ScrapeData.arel_table
  suggestions = ArtistNameSuggestion.arel_table
  where(ArtistNameSuggestion.where(
    suggestions[:original].eq(scrape_datas[:artist_name])
  ).exists.not)
end

现在您可以执行以下操作:

ScrapeData.not_found.blacklisted.extracted.no_suggestions
于 2012-11-28T04:21:46.720 回答