Luigi
Luigi

Reputation: 5603

Select Unique Model Attributes - Rails

I have a model list, which has attributes name and id.

I want to select every unique name attribute across all list models.

So if my list models look like this:

list1

id: 1
name: ABC

list2

id:2
name: DEF

list3

id:3
name: ABC

I want to end up with an array like so ['ABC','DEF']. What's the easiest way to do this?

Upvotes: 0

Views: 81

Answers (1)

Kevin Sjöberg
Kevin Sjöberg

Reputation: 2254

I would go with

List.select(:name).distinct

See http://guides.rubyonrails.org/active_record_querying.html for more information about the ActiveRecord query interface.

Upvotes: 1

Related Questions