2017 week 1 Flashcards
What is the difference between a ‘collection route’ and a ‘member route?’
A member route requires an ID, because it acts on a member.
A collection route doesn’t require an ID because it acts on a collection of objects.
What does a ‘collection route’ and a ‘member route’ have in common?
They’re both ways to add additional actions to a resource-based route in Rails.
What is ORM?
Object Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.
What does Active Record as an ORM Framework allow us to do?
Active Record gives us several mechanisms, the most important being the ability to:
Represent models and their data.
Represent associations between these models.
Represent inheritance hierarchies through related models.
Validate models before they get persisted to the database.
Perform database operations in an object-oriented fashion.
How Active Record fits into the Model-View-Controller paradigm?
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.
What command line of code would you use to commit the following record to the database?
user = User.new
user. name = “David”
user. occupation = “Code Artist”
user.save
What 2 methods return false when validation fails
save
update
they return false when validation fails and they didn’t actually perform any operation on the database.
What is the schema.rb file?
It shows the up-to-date structure of your database.
What 2 columns are created by t.timestamps in the database
The timestamps macro adds two columns, created_at and updated_at.
What does the following command do?
$ bin/rails db:rollback STEP=3
revert the last 3 migrations.
What are The Golden Three Rules of using ‘self’?
Use self when setting/getting instance attributes inside a class definition. Use self to denote a method within the class definition as a class method. Use self to reference the calling object within an instance method definition.
Class methods vs instance methods
instance methods are behaviors that can only be operated by an instance of a particular class, and class methods are behaviors that can only be operated by the class object itself.
How would you verify whether or not a particular attribute of an object is valid?
You can use errors[:attribute]. It returns an array of all the errors for :attribute. If there are no errors on the specified attribute, an empty array is returned.
This method is only useful after validations have been run, because it only inspects the errors collection and does not trigger validations itself. It’s different from the ActiveRecord::Base#invalid? method explained above because it doesn’t verify the validity of the object as a whole. It only checks to see whether there are errors found on an individual attribute of the object.
How can you check which validations failed on an invalid attribute?
errors.details[:attribute]. It returns an array of hashes with an :error key to get the symbol of the validator
How do you manually write the initial database migration for a Post model with attributes title:string and body:text?
class CreatePosts < ActiveRecord::Migration[5.0] def change create_table :posts do |t| t.string :title t.text :body
t.timestamps end end end