nish
nish

Reputation: 7280

How to get a unique set of values for a given field of array

I am working on integrating Solr search with a RoR app. The solr returns docs which have a field productTaxonomyName.

In the controller corresponding to search I retrieve docs from solr and push them into a @products array. I want to get the set of unique values of the productTaxonomyName field in a variable within the controller. How can I do that.

Will this work:

@taxons = @products.map {|p| p.productTaxonomyName }

Thanks

Upvotes: 0

Views: 45

Answers (1)

Amol Pujari
Amol Pujari

Reputation: 2349

@taxons = @products.map(&:productTaxonomyName).compact.uniq

Btw, why dont you get them right from Solr.

Upvotes: 1

Related Questions