Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive group_by in Rails?

ok so i have this call

location.requests.group_by(&:song)

location is

>> 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)

this call is returning two records "One" and "one" because they are saved that way in the db....any idea on how to redo the group_by to only return one record with both

I am using sqlite

like image 581
Matt Elhotiby Avatar asked Nov 18 '10 18:11

Matt Elhotiby


1 Answers

Group_by can also take a code block. So instead of:

location.requests.group_by(&:song)

Do:

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

See here for pertinent documentation.

like image 57
Roadmaster Avatar answered Sep 27 '22 22:09

Roadmaster