user1576738 user1576738 - 10 months ago 51
Ruby Question

Display select Item only if match is found

I'm trying to match records from a recordset against an array of strings. If the recordset returns a match against the string then exclude those records and do not show them in the select dropdown (almost like a filter). I know I could potentially use "reject" to remove key/value pairs from a hash but I don't know how to combine it with "collect". Any help is appreciated.

### string to compare against
ACCOUNT_NAMES = %w(aws-1 aws-2 aws-3 aws-4)

### In the controller
def aws_accounts
amazon_accounts_all = Amazonaccount.all
@amazon_accounts = amazon_accounts_all.collect {|p| [, ] }

Answer Source

ActiveRecord provides the necessary querying tools to solve this:

@amazon_accounts = Amazonaccount.where.not(name: ACCOUNT_NAMES).pluck(:id, :name)