在Rails中不区分大小写的group_by?

时间:2010-11-18 18:16:21

标签: mysql ruby-on-rails ruby sqlite

好的,所以我有这个电话

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)

这个调用返回两个记录“One”和“one”,因为它们以这种方式保存在db中....任何想法如何重做group_by只返回一个记录

我正在使用sqlite

1 个答案:

答案 0 :(得分:10)

Group_by也可以采用代码块。所以而不是:

location.requests.group_by(&:song)

执行:

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

有关相关文档,请参阅here