Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Last active December 18, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jridgewell/5760666 to your computer and use it in GitHub Desktop.
Save jridgewell/5760666 to your computer and use it in GitHub Desktop.
Polymorphic select
class Catch < ActiveRecord::Base
belongs_to :animal, :polymorphic => true
end
class Bird < ActiveRecord::Base
attr_accessible :name
has_one :catch, :as => :animal
end
class Fish < ActiveRecord::Base
attr_accessible :name
has_one :catch, :as => :animal, :dependent => :destroy
end
<select>
<optgroup label="Fish"><!-- That's a polymorphic class -->
<option value="Fish_1">Trout</option>
<option value="Fish_2">Salmon</option>
</optgroup>
<optgroup label="Bird"><!-- That's another polymorphic class -->
<option value="Bird_1">Eagle</option>
<option value="Bird_2">Pigeon</option>
</optgroup>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment