option_groups_from_collection_for_select with a ActiveRecord::Base single object

Rails documentation gives examples of using option_groups_from_collection_for_select with two tables/objects.

How could it be done with one?

Example that almost works:

I have a form to enter address from both the United States and Canada. It has the following:

====new.html.erb==== <%= select_tag(   :state,   option_groups_from_collection_for_select(     State.countries,     :country,     :country,     "id",     "id")) %>

Just use options_from_collection_for_select

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW! http://sensei.zenunit.com/

Ok, that and some reading has gotten me to this:

====app/views/wherever/new.html.erb==== <%= render(:partial => "states/state") %>

The only example that I've seen of this is in AWD Ch. 22 (p. 488 printed), and it seems to be a lot of work. The book wraps the option_groups_from_collection_for_select in <select> tags (html, not a helper). The first parameter given is the highest level array - in your case, it would essentially be [ "Canada", "US" ] - but each value in the array is another object (a class in the example) with the group name and sub-options available through attr_reader methods.

It seems like a lot of work. If you want to do it the "good" way, read more of the API on option_groups_from_collection_for_select (I think that you were on the right track for what you want to do). Since you only have two groups, you could also do a little hard-coding and use helpers to get the states.

-Kyle