I have a TargetMarket class that has been seeded with all of the countries in the world like so
TargetMarket.create([
{name: 'Andorra'},
{name: 'United Arab Emirates'},
{name: 'Afghanistan'},
{name: 'Antigua and Barbuda'},
....
....
{name: 'South Africa'},
{name: 'Zambia'},
{name: 'Zimbabwe'}
])
A user can then select up to 5 countries they wish to have as a target market for their Company.
On the public search page, I have a dropdown selection of all the TargetMarkets.
The current code reads as
<%= f.select :target_markets_id_in, TargetMarket.all.map{ |u| u.name, u.id] }, { include_blank: "All" }, {class: 'selectize-this', multiple: true} %>
However, this obviously shows up ALL of the countries. I only want the countries that have been used as a target market by a company to populate the dropdown.
For example; A company has target markets of "Ireland", "Belgium", "Australia" and "Japan".
On the target_markets search option, I only want Ireland, Belgium, Australia and Japan to appear as possible search options as they are the only countries used in the database.
Is this possible?
Something like
<%= f.select target_market_ids_in, TargetMarkets.where('name' count >= 1) %>
Edit #
Relationship
class Company < ApplicationRecord
has_and_belongs_to_many :target_markets
accepts_nested_attributes_for :target_markets, allow_destroy: true
end