Created
April 2, 2012 00:26
-
-
Save jaylevitt/2279656 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :user_pets | |
has_many :pets, :through => :user_pets | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%# 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(',') %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%# 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