0
votes

I am trying to bind Categories drop down on Posts Search page. The drop down should display only those Categories which are referred in Posts table.

Following line binds ALL Cetegories:

<%= f.collection_select(:cat_id_eq, Category.order(:name), :id, :name, :include_blank => 'Any') %>

Where as following line does NOT bind the list of unique Categories (ONLY those which are referred in Posts table, that is what i am looking for)

<%= f.collection_select(:cat_id_eq, Post.uniq.pluck(:vendor_id).compact.sort, :id, :name, :include_blank => 'Any') %>

NOTE: Post belongs_to Category and Category has_many Posts.

Any help is appreciated.

1

1 Answers

1
votes

Try this:

<%= f.collection_select(:cat_id_eq, Category.joins(:posts).uniq, :id, :name, :include_blank => 'Any') %>