0

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 

2 Answers2

1

JOINing and DISTINCTing will give you a unique list of objects with associations. This is because the default Rails' ActiveRecord join is an INNER JOIN, which filters for the presence of the association. ActiveRecord will still only SELECT columns from the original table, so DISTINCT will return a unique list of objects.

In your case, you need to add a habtm association to TargetMarket if it does not already exist:

class TargetMarket < ApplicationRecord
  has_and_belongs_to_many :companies
end

Then replace this line:

TargetMarket.all.map{ |u| u.name, u.id] }

with:

TargetMarket.joins(:companies).distinct.map { |u| [u.name, u.id] }

While you're at it, you may want to take a peek at the result of

TargetMarket.joins(:companies).distinct.to_sql
Aaron Breckenridge
  • 1,723
  • 18
  • 25
0

When a user selects up to 5 countries they wish to have as a target market for their Company, do you store that selection as an association between the Company and the target market? If you do, then you can return only the target markets associated with the current company

class Company < ActiveRecord::Base
    has_many :target_markets
end

Adding the target markets

@company.target_markets << TargetMarkets.find_by_name('Finland')

Displaying the target markets

# assuming `@company` is set based on the route (`/company/:id`), `current_user` settings, or however you determine what the current company is
<%= f.select :target_markets_id_in, @company.target_markets.map{ |u| u.name, u.id] }, { include_blank: "All" }, {class: 'selectize-this', multiple: true} %>

Further reading

alexanderbird
  • 3,847
  • 1
  • 26
  • 35