【问题标题】:Case insensitive group_by in Rails?Rails中不区分大小写的group_by?
【发布时间】:2011-05-12 05:06:09
【问题描述】:

好的,我有这个电话

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

【问题讨论】:

    标签: mysql ruby-on-rails ruby sqlite


    【解决方案1】:

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

    location.requests.group_by(&:song)
    

    做:

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

    有关相关文档,请参阅 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-18
      • 2010-10-15
      • 2013-06-23
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      相关资源
      最近更新 更多