2

I'm using Postgresql with ActiveRecord in Rails 4.

I have a customer model and one of the columns is called "tags" and it is an array column (["sports", "broadcasting"]).

How can I select all the distinct values from this column? I would like to avoid doing anything where I would have to instantiate AR objects due to the amount of customer records we have. So I don't want something like:

Customer.select(:tags).map(&:tags).flatten.uniq

which works but uses too much memory.

I need the values to provide suggestions when someone is adding a tag to a customer. Hopefully it will help prevent variations of words or misspellings.

Thanks in advance!

1
  • 2
    Try Customer.pluck(:tags).flatten.uniq Commented Jul 16, 2015 at 4:57

1 Answer 1

3

You can use pluck like below

Customer.pluck(:tags).flatten.uniq
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.