Forum: Ruby on Rails Scoping option_groups_from_collection_for_select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A822370edefad22484f9260fb787c710?d=identicon&s=25 Ed (Guest)
on 2008-11-18 14:42
(Received via mailing list)
How can I scope the options returned by
option_groups_from_collection_for_select?

Method calls look like this:  option_groups_from_collection_for_select
(collection, group_method, group_label_method, option_key_method,
option_value_method, selected_key = nil)

But say I want to limit the results returned by the option_key and
option_value methods to some particular scope?

For instance, using the sample code in rdoc:

  class Continent < ActiveRecord::Base
    has_many :countries
    # attribs: id, name
  end
  class Country < ActiveRecord::Base
    belongs_to :continent
    # attribs: id, name, continent_id
  end

I could get all countries grouped by continent with this:
  option_groups_from_collection_for_select
(@continents, :countries, :name, :id, :name, 3)

But say I had another class called Ally:

  class Alliance < ActiveRecord::Base
    has_many :countries   #reciprocal belongs_to added to country
    #attribs: id, name
  end

How could I scope the OGFCFS call so that it only listed countries
belonging to a particular alliance?
This topic is locked and can not be replied to.