media
media

Reputation: 135

Count distinct with activerecord ruby

The table Visit contains the column extra which is written this way user:xxx login:yyy (string type).

I want to determine all the number of distinct passwords (after the word login). I wrote this ActiveRecord query in my controller.

Visit.select(:extra).where('extra LIKE ?', "%login%").distinct.count

This query will give me a result of distinct extra which contains the word login not the password (yyy in this example). I want to do the same but for all the strings (passwords) after the word login.

Is there any solution to do this? Any help will be appreciated.

Upvotes: 1

Views: 213

Answers (1)

31piy
31piy

Reputation: 23859

You can use MySQL's SUBSTRING_INDEX to achieve this.

Visit.select("SUBSTRING_INDEX(extra, ':', -1)").where('extra LIKE ?', "%login%").distinct.count

Upvotes: 2

Related Questions