Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:01:17+00:00 2026-06-10T17:01:17+00:00

I have two collection_select drop down menus that are currently set up like so:

  • 0

I have two collection_select drop down menus that are currently set up like so:

<div class="field">
    <%= f.label :country_cont, "Country" %>
    <%= f.collection_select(:country_cont, Country.all, :country, :country) %>
</div>

<div class="field">
    <%= f.label :city_cont, "City" %>
    <%= f.collection_select(:city_cont, Job.all, :city, :city) %>
</div>

What I would like is for the drop down to only display a list of unique values from my database tables (for example in City rather than displaying London 5 times because there are 5 rows that have that city I would like London to only be listed once).

Any advice on this would be much appreciated! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:01:18+00:00Added an answer on June 10, 2026 at 5:01 pm

    I would create a scope in your Job class that filters by unique city. How you actually implement this will depend on your database. The example below uses MySQL syntax:

    class Job
      scope :unique_by_city, lambda { select('DISTINCT(city)') }
    end
    

    In your view

    <div class="field">
        <%= f.label :city_cont, "City" %>
        <%= f.collection_select(:city_cont, Job.unique_by_city, :city, :city) %>
    </div>
    

    An alternate version would be if you don’t have many Jobs in your database, just load all the records and do it in ruby:

    class Job
      def self.unique_by_city
        all.uniq_by(&:city)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two entities that are related like so. public class User {
I have two elements that I would like to select <input id=iMe /> and
I have two models, Category and Post. Category.rb class Category include Mongoid::Document field :title,
In my form, I have two selects - state and city. The city drop-down
I have two drop down list in my webpage and one is dependent on
I currently have a ViewModel set up for a blog: public class PostViewModel {
I have two models Doctors and Questions like the follwong: Doctor Model class Doctor
I have two classes and mapping for the collection: class User { Guid ID;
I have a class which has two HashSet<String> collections as private members. Other classes
I have two sections in a form, and I have a button that toggles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.