ActiveRecord @grid가 있습니다.ID 배열을 기반으로 활성 레코드 정렬
해시 @ 번호가 있습니다.
1.9.3p385 :086 > Grids
=> Grids(unique_id: integer, price: float, availability: string,
delivery: string, delivery_time: string, delivery_cost: float,
special_offers: text, warranty: text, created_at: datetime, updated_at: datetime)
=> @numbers
=>{7=>114, 9=>21, 12=>7, 13=>31, 14=>51, 16=>43, 18=>48, 21=>6, 22=>18,
24=>69, 27=>47, 30=>47, 32=>31, 33=>36}
@numbers 해시 UNIQUE_ID = 지나지> 클릭 수
1.9.3p385 :091 > @no = @numbers.sort_by{|k,v| v}.reverse.map{|i| i[0]}
=> [7, 24, 14, 18, 30, 27, 16, 33, 13, 32, 9, 22, 12, 21]
@no가 UNIQUE_ID의 배열 없다
레일 본체.
@grid = Grid.all
@grid is an activeRecord, I want to sort this active record based
on the order of @no which is nothing but the unique_id in @grid.
임
@grid.sort_by{ |h| @no.index(h.unique_id) }
그것이라고, 작동하지 않는, 이해
ArgumentError: comparison of NilClass with 14 failed
가는 일부 전무 비교가,이 시도 에. 이것을 무시하는 방법이나 더 나은 접근 방법이 있습니까?
m_x, 당신의 해결책에 종사하는 Im, 그것의 종류의 빙고. 내가 그것을 밖으로 시험해보고, 뒤로 걸어 가자! – beck03076
야, 어떻게 결과 배열에서 nils를 무시할 수 있을까? – beck03076
닐을 잊어라, 나는 그것을 알아낼 것이다. 당신의 솔루션은 아름답게 작동했습니다. 감사. – beck03076