Rails相当于这个复杂的sql查询
这是最初的逻辑Rails相当于这个复杂的sql查询,sql,ruby-on-rails,arel,Sql,Ruby On Rails,Arel,这是最初的逻辑 (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.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
)
尽管在将“and not EXISTS”查询放入混合中时遇到问题
and not EXISTS(
SELECT * FROM artist_name_suggestions where original = artist_name
)
谢谢 首先,您可以提取简单作用域:
scope :not_found, where(:artist_status => 'NOT_FOUND')
scope :blacklisted, where(:blacklisted => 1)
scope :extracted, where(:extracted => 0)
然后添加一个查询方法(假设Artister_name是一列scrape_数据):
现在您可以执行以下操作:
ScrapeData.not_found.blacklisted.extracted.no_suggestions
首先,您可以提取简单的作用域:
scope :not_found, where(:artist_status => 'NOT_FOUND')
scope :blacklisted, where(:blacklisted => 1)
scope :extracted, where(:extracted => 0)
然后添加一个查询方法(假设Artister_name是一列scrape_数据):
现在您可以执行以下操作:
ScrapeData.not_found.blacklisted.extracted.no_suggestions
很好,这在Rails 3中工作得很好(正如我测试的那样)。尽管不幸的是,这个应用程序需要与rails 2.3.5结合。我有假的arel可以处理这个范围,虽然因为我没有arel的访问权限(我不能做arel_表)。你知道Rails2.3.5如何做到这一点吗?太好了,这在Rails3中工作得很好(正如我测试的那样)。尽管不幸的是,这个应用程序需要与rails 2.3.5结合。我有假的arel可以处理这个范围,虽然因为我没有arel的访问权限(我不能做arel_表)。你知道Rails 2.3.5如何做到这一点吗?