2013-03-27 4 views
0

에서 데이터를 가져 오는 후 나는 내 레일 컨트롤러루비 - Splititng 배열 MySQL의

@main = $connection.execute("SELECT * FROM builds WHERE platform_type IS NOT NULL") 

이 내가 생각 배열처럼 동작 mysql2 타입의 객체를 반환으로 MySQL 데이터베이스에서 데이터를 가져 오기 위해 다음과 같은 코드가 있습니다.

이 배열을 2 개의 배열로 나누고 싶습니다. 첫 번째 배열은 platform_type이 'TOTAL'이고 다른 배열의 배열은 모두 배열로 나누고 싶습니다.

답변

2

그것은 실제로 Mysql2::Result 개체를 반환합니다.

Builds.where("platform_type = ?", 'TOTAL') 
Builds.where("platform_type NOT IN ?", [nil, 'TOTAL']) 
+0

http://chat.stackoverflow.com/rooms/24194/ruby에 가입하시기 바랍니다. –

+0

# Mysql2 :: Result : 0xb73e9790> –

+0

@sagarvikani Oooups에 대한 정의되지 않은 메소드 'each_hash', 'mysql2'내에서 메소드는 'each_hash'가 아닌 'each'라고합니다. 내 잘못. – mudasobwa

-1

array.select를 사용해보십시오.

http://matthewcarriere.com/2008/06/23/using-select-reject-collect-inject-and-detect/

total = @main.select { |build| build.platform_type == 'TOTAL' } 
not_total = @main.reject { |build| build.platform_type == 'TOTAL' } 
같은 뭔가 더 나은,이 답변에 따라 Enumerable.partition를 사용 Ruby Select and Reject in one method

+0

Uhmmm ...'MySQL의 :: Result'는'Array' 자손 실제로되지 않습니다 : 물론

totals = [] others = [] main.each { |r| (r['platform_type'] == 'TOTAL' ? totals : others) << r } 

을 할 수 있지만, 왜 같은 떨어지게와 레일의 방법을 사용하지 마십시오. – mudasobwa