jackerman09
jackerman09

Reputation: 2630

Grouping a Rails collection_select by a field's value

I would like to have the following collection_select be grouped by State, which is a field on Tuning that is either Public or Private. Is this possible?

In the view:

<%= collection_select :tunings, :tuning, @fretboard_tuning_options, :id, :name, {}, { :multiple => false, class: "", id: "tuning", style: 'width: 100%;' } %>

In the controller:

@fretboard_tuning_options = Tuning.where('state=? OR user_id=?', 'Public', current_user.id)

Any tuning with a State of Public will not overlap with those that have user_id = current_user.id. The goal is to have a dropdown box (I am using Select2 to format the dropdown) that shows two groups of options: Public and Private, and under each group are the relevant Tunings. Is this possible?

Thanks!

Upvotes: 1

Views: 821

Answers (1)

Related Questions