由於場地有一個字段「featured_level」,可以從0到N.排序屬性和隨機復發
我需要返回由featured_level,下令但具有相同的隨機場館的秩序場館featured_level。
UPDATE:
使用給出答案,我有這樣的方法:
def self.by_featured_level
all.group_by {|v| v.featured_level}.inject([]) { |memo, (level,values)|
memo << { level => values.shuffle }
}.map { |hash| hash.values }.flatten.reverse
end
但它沒有下面的測試(也沒有實數的正常工作),我還在努力算起來,使用的測試:
describe "by featured level" do
before do
@venue1 = create(:venue, featured_level: 5)
@venue2 = create(:venue, featured_level: 2)
@venue3 = create(:venue, featured_level: 4)
@venue4 = create(:venue, featured_level: 2)
@venue5 = create(:venue, featured_level: 0)
@venue6 = create(:venue, featured_level: 2)
@venues = Venue.by_featured_level
end
it {
start_with_hightest = @venues.index(@venue1) == 0
expect(start_with_hightest).to be_truthy
}
it {
second_hightest_is_2nd = @venues.index(@venue3) == 1
expect(second_hightest_is_2nd).to be_truthy
}
it {
ends_with_lowest = @venues.last.id == @venue5.id
expect(ends_with_lowest).to be_truthy
}
end
我得到這個錯誤: TypeError:沒有將符號隱式轉換爲整數 完整測試: http://pastebin.com/XgW3RnjM – fabriciofreitag
是的,提出了一些錯誤。嘗試修訂版本。 – zetetic
仍然無法正常工作,我更新了實施和測試的代碼。 – fabriciofreitag