Skip to content

Instantly share code, notes, and snippets.

@jaylevitt
Created April 2, 2012 00:26
Show Gist options
  • Save jaylevitt/2279656 to your computer and use it in GitHub Desktop.
Save jaylevitt/2279656 to your computer and use it in GitHub Desktop.
class Pet < ActiveRecord::Base
has_many :user_pets
belongs_to :user, :through => :user_pets
validates :name, :allow_nil => true, :inclusion => { :in => Pet.valid_pets }
def self.valid_pets
['fish',
'bird',
'tree',
]
end
end
class User < ActiveRecord::Base
has_many :user_pets
has_many :pets, :through => :user_pets
end
class UserPet < ActiveRecord::Base
belongs_to :user
belongs_to :pet
end
class CreateUserPets < whatever::migration
create_table :user_pets do |t|
t.integer :user_id
t.integer :pet_id
end
<%# I don't remember select_for_options or select_for_options_from_hash syntax, so I'll just display the list of valid pets here for you: %>
<% Pet.valid_pets.join(',') %>
class Pet
belongs_to :user, :through => :users_pets
validates :name, :allow_nil => true, :inclusion => { :in => Pet.valid_pets }
def self.valid_pets
['fish',
'bird',
'tree',
]
end
end
<%# I don't remember select_for_options or select_for_options_from_hash syntax, so I'll just display the list of valid pets here for you: %>
<% Pet.valid_pets.join(',') %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment