Matt Elhotiby
Matt Elhotiby

Reputation: 44066

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

Upvotes: 5

Views: 1303

Answers (1)

Roadmaster
Roadmaster

Reputation: 5357

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.

Upvotes: 11

Related Questions