w3d3 Flashcards
In Rails, how do you configure a database and create it?
Edit it in config/database.yml, then run rake db:create
What is a migration?
A file containing Ruby code that describes a set of changes applied to the database
When making a class that handles a migration, what must it inherit from?
ActiveRecord::Migration
In ActiveRecord::Migration, what does ‘string’ refer to?
VARCHAR(255)
What is ActiveRecord::Migrations#change do, and how does it relate to #up and #down?
change can migrate or rollback a database. #up calls change to migrate, #down calls change to rollback.
Where are migration files stored?
db/migrate
What exactly does db:migrate do?
It finds unexecuted migrations, then runs their up or change methods.
Where does Rails store information about which migrations have been run?
In a table called schema_migrations.
T/F: After you run a migration, you can edit the migration and then run it again.
F; you must rollback the db and run the migration again; Rails will already think you ran the migration and will not run it again otherwise. YOU SHOULD ROLLBACK FIRST AND THEN EDIT THE MIGRATION, b/c otherwise Rails will try to roll back the db in its current version, which may not be compatible.
When should you ever edit an existing migration?
When it has never been run and/or pushed to git.
Where can you see the current state of your db’s schema?
db/schema.rb
What is an ORM?
An object relational mapping is the system that translates between SQL records and Ruby (or Java, or Lisp…) objects. The ActiveRecord ORM translates rows from your SQL tables into Ruby objects on fetch, and translates your Ruby objects back to rows on save.
In essence, it allows Ruby objects to interact with a db.
For each table in the db, what is defined?
A model (given the singular name of the table). Each instance of the model class represents a row in the db. All of these classes inherit from ActiveRecord::Base
T/F: Ruby automatically distinguishes between the plural name of a db and the singular name of a model.
T; it recognizes the relation and associates them.
What do ActiveRecord::Base::find and ActiveRecord::Base::all do?
::all returns all objects of the model that are in the table (one instance per row)
::find takes an integer argument, and finds the object whose primary key is the argument.
What does ActiveRecord::Base::where do?
It turns the string arguments given into a WHERE clause, then retrieves the filtered list of objects.
How does the following syntax avoid an SQL injection attack:
Physician.where(“home_city = ?”, “La Jolla”)
It uses the value “La Jolla” to stand in for the ‘?’ in the first string argument. It escapes every character in “La Jolla” to avoid it being interpreted as malicious code (if it’s given)
What is an alternate way to pass a SQL fragment?
Physician.where(:college => [“City College”, “Columbia”, “NYU”])
Physician.where(:years_experience => (3..9))
T/F: ActiveRecord::Base will give each model class attr_accessors for each column
T
After we make changes to a model instance, how can we save those changes (read: change the corresponding row)?
Call #save! on the instance
How can we remove an instance (row) from the db altogether?
Call #destroy on the instance