2010-11-18 53 views
3

行,所以我有這個電話不區分大小寫group_by in Rails?

location.requests.group_by(&:song) 

位置

>> location = Location.find 4 
=> #<Location id: 4, venue: "Rod Laver Arena at Melbourne Park - Melbourne Vic, ...", 
showdate: "2010-11-20", created_at: "2010-10-28 01:20:42", updated_at: 
"2010-10-28 01:20:42", band_id: nil, artist_name: "Metallica"> 


location.requests.group_by(&:song) 

此調用返回兩個記錄「一」和「一個」,因爲它們被保存在數據庫中這樣....如何重做GROUP_BY任何想法只返回一個記錄既

我使用SQLite

回答

10

GROUP_BY也可以採取一個代碼塊。因此,而不是:

location.requests.group_by(&:song) 

務必:

location.requests.group_by{|i| i.song.downcase} 

的相關文件見here