venkat
venkat

Reputation: 836

How to show unique records in Rails?

There is a model named Company and it is having number of records. Later I added a field named area. I search field, i am adding this field too.

I am displaying all the areas in a drop down as follows:

<%= label_tag :area, "Area" %>
<%= select_tag 'area', options_for_select(Company.uniq.pluck(:area)),include_blank: true, class: 'form-control'} %>

Now areas are displaying fine but when I give area as "us" in one case and "US" in another case and "Us" in other case, it is displaying 3 fields and also the previous records will be having area fields as empty/blank, here it is showing 2 or more blanks.

How to show unique records for capital and downcase and how to show only one blank in drop down?

Upvotes: 1

Views: 74

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34336

Change this:

Company.uniq.pluck(:area)

to this:

Company.pluck(:area).compact.map(&:downcase).uniq

This will give you uniq downcased areas. i.e. you will get only us instead of three options: US, us and Us.

Upvotes: 1

Related Questions