1

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!

jackerman09
  • 2,492
  • 5
  • 29
  • 46

1 Answers1

2

You want grouped_collection_select: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_collection_select

David Underwood
  • 4,908
  • 1
  • 19
  • 25
  • 1
    From that link, it looks like `grouped_collection_select` is for use with a `has_many` relationship, not when grouping by the value of a field on a single model. Is this usable in my case? Thanks – jackerman09 Mar 21 '14 at 14:20