Models: Rails for Zombies 1 2 - mikesabat/LR_HF GitHub Wiki
Models are the way that Rails connects to the database. The model is a Class which maps to it's name, plural as a table. Tweet.find(id) will look in the Tweets table by default.
Validates
In the model, you can make sure that an attribute validates.
validates :attribute, :presence => true - will not save the record without :attribute
validates :attribute, :numericality => true - will only save a number
validates :attribute, :uniqueness => true - so there are no dupes
validates :password, :confirmation => true
validates :password, :length => {:minimum => 3, :maximum => 100}
validates_format_of :email, :with => /regex/i
validates_inclusion_of :age, :in => 21..99
validate_exclusion_of :age, :in => 0..21, :message => "Sorry, you must be 21"
Relationships
This is how tables connect. Not yet sure how to decide when to build a new table. Tables can belong to another table, or have instances of another table. Inside of the model - belongs_to :zombie or has_many :tweets
Setting up relationships allows you to call data easier from the counsel.
t = Tweet.find(1) t.zombie >>>> will return the zombie that the tweet belongs to.