1. Introduction to Databases
Introduction to working with data through databases
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
Models are great. In addition to quickly scaffolding out connections to our database, we can also do lots more with them. One thing that we can do (and it’s really useful) is to add validations to our models.
Imagine you’re adding information to our database, we’re adding a task. Before we actually create a record, we’ll want to make sure our data is what we’re expecting. For example, we’ll want to check
We can use validates_presence_of
to check that a required field has been added to our model before we create it or save any changes to it.
validates_presence_of :name
validates_presence_of :list_id
We can check for the length of a string too. It provides a variety of options, so you can specify length constraints in different ways:
validates :name, length: { minimum: 2 }
validates :bio, length: { maximum: 500 }
validates :password, length: { in: 6..20 }
validates :registration_number, length: { is: 6 }
If we had a user table and we wanted to make sure that each account was linked to one unique email we could do the following:
validates :email, uniqueness: true
If we wanted to check that the year was in a valid range, we could do something like
validates :year, :numericality => { :greater_than => 1970, :less_than_or_equal_to => 2016 }
or we could also do
validates :year, :inclusion => { :in => 1990..2020 }
validates :age, :inclusion => { :in => 1..100 }
After you create or save an record you can use the valid
method to see if any validations failed.
task = Task.create(name: "My Task")
task.valid? # => true
task = Task.create(name: nil)
task.valid? # => false
ActiveRecord also create some really nice human readable error messages for you by default if there’s any validations that fail. You can check them like so:
task.errors.messages
# => {name:["can't be blank"]}
Validations are the only way to ensure integrity of your data. If you’re adding forms or allowing anyone to pass data to your application, you need to make sure that only the right kinds of information gets in. It prevents errors and/or people hacking bad info into your database. And no one wants that, so you should make sure you apply validations to every model you create!
This series guides you through working with databases, from setting up and installing your database, storing and retrieving data and deploying to heroku
Introduction to working with data through databases
Getting started - installing your database engine
Getting started - adding dependencies to your project
Getting started - adding activerecord rake commands
Migration files are small Ruby scripts that make changes to your database
Schemas define the structure of your database table and allow ActiveRecord to structure requests and changes for data
Use Models to map a database table onto native ruby objects.
Linking Models to Routes. Making a CRUD API
Linking Models to Models - Adding Associations
How to deploy your project to Heroku and add a database
How to check the information you're adding to the database
How to create initial data with a Seed File