2012-08-06 5 views
0

레일에서 ActiveRecord에서 다음 쿼리를 작성하려고합니다.레일 용 ActiveRecord 쿼리

SELECT * FROM test_run WHERE build = '$build' AND suite='$suite'AND (result = 'fail' OR  result = 'error') 
    AND test_name NOT IN(SELECT test_name FROM test_run WHERE result = 'pass' AND build = '$build')GROUP BY test_name"; 

첫 번째 부분은 쉽습니다. 부역을 수행하는 방법을 잘 모르겠습니다.

scope :never_passed, lambda { |b| where(:build => b, :status => 'fail').where(??) 

위의 SQL에서 작동하도록이 하위 쿼리를 만들려면 어떤 작업을해야합니까? Arel에

감사

+0

체크 이것을. 그것은 당신에게 두통을 저장해야합니다 : https://github.com/ernie/squeel/ – AJcodez

답변

1

감사합니다, 당신은 같은 서브 쿼리를 수행 할 수 있습니다

TestRun.where(:test_name => TestRun.where('result != ? && build != ?','pass','somevalue').select('test_name')).to_sql 

것 출력 다음 SQL :

SELECT `test_runs`.* FROM `test_runs` WHERE `test_runs`.`test_name` IN ('test1','test2')