Is there a way to run a query in RoR that would have the same results as the following?
SELECT SUBSTRING_INDEX(notes, ',' , 1) AS notes FROM <table> WHERE event = 'Bar'
I’ve tried and tried but the substring_index part seems to not be friendly with the find_by_sql function.
Update This is a more accurate portrayal of the query I am attempting to make.
SELECT SUBSTRING_INDEX(notes, ',', 1) notes, COUNT(*) cnt FROM <table> WHERE event = 'Foo' GROUP BY SUBSTRING_INDEX(notes, ',', 1) ORDER BY COUNT(*) DESC
I’m close with this call:
Foo.where("event = 'Foo'").group('notes').order('count_all').count()
But it’s obviously not quite right.
1 Answer