I have this active record query:

current_user.company.properties.pluck(:name).uniq

what I would like to do is get this query but only for properties with 'name' that occur at least twice. how would i do that?

有帮助吗?

解决方案

You can group by name and add a having call as well:

...properties.group(:name).having("count(name) > 1").pluck(:name)

There's no longer any need for the uniq call because, as you're grouping by name, you'll only get each name returned once.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top