Skip to content

Instantly share code, notes, and snippets.

@Osmose
Created January 29, 2011 02:51
Show Gist options
  • Save Osmose/801456 to your computer and use it in GitHub Desktop.
Save Osmose/801456 to your computer and use it in GitHub Desktop.
# Class for a table called "zombies" in MySQL with some columns
class Zombie < ActiveRecord::Base
validates_presence_of :name #name column must exist before saving to DB
validates_uniqueness_of :name #names must be unique
has_many :weapons #The weapons table will have a column describing ownership by zombies
end
# Class for "weapons" table
class Weapon < ActiveRecord::Base
belongs_to :zombie #Other half of ownership, column will be called "zombie_id"
end
# Get a list of all weapons that the zombie with id 3 owns
w = Zombie.find(3).weapons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment