w3d3 rails Flashcards

1
Q

How do you run migrations

A

bundle exec rails db:migrate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a database with rails

A

set up config/database.yml

bundle exec rails db:create

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are 5 gems to add to rails for development

A
annotate
better_errors
binding_of_caller
byebug
pry-rails
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you run migrations?

A

rails db:migrate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where can you see a copy of your database’s current schema as reported by rails?

A

db/schema.rb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the best/fastest way to initialize a new database? What does this rely on?

A

rails db:schema:load

db/schema.rb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you enforce that a column must have a value?

A

within create_table:

t.string :name, null: false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between belongs_to and has_many ?

What do they have in common?

A

belongs_to exists on the model that contains the primary key

has_many exists on the other model

They allow the use of helper instance methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What arguments are passed to a belongs_to or has_many association?

A

(trick) answer: two

better answer: we use ruby’s ability to use a hash as a final argument without braces to pass several options to the association method like so:

belongs_to :table_name, primary_key: :id,
foreign_key: :foreign_key_id,
class_name: ‘SomeClass’

example:

belongs_to :house,
brimary_key: :id,
foreign_key: :house_id,
class_name: ‘House’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What arguments are passed to a belongs_to or has_many association?

A

(trick) answer: two

better answer: we use ruby’s ability to use a hash as a final argument without braces to pass several options to the association method like so:

belongs_to :table_name, primary_key: :id,
foreign_key: :foreign_key_id,
class_name: ‘SomeClass’

example:

belongs_to :house,
brimary_key: :id,
foreign_key: :house_id,
class_name: ‘House’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does this do:

validates :attribute, presence: true

A

It validates that a particular attribute exists on a method when we call #save

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we validate the uniqueness of an attribute for an object?

A

Add this to a validates macro:

uniqueness: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do we create a custom validator for a model?

A
  1. create a method that conditionally shovels an error message into a new key on the self.errors hash
  2. add this line:

validate :my_validator_method_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do we see the errors for an object?

A

errors.full_messages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s the difference between ApplicationModel#save and #save!

A

save will return false if there are errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the difference between ApplicationModel#save and #save!

A

save will return false if there are errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How would you use a symbol as a value in a SQL string?

A

awesome_symbol: 55

<

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are two levels of data integrity-protection in rails?

A

DB-level constraints

Model-level validations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Why shouldn’t you roll back a migration?

A

Can make things out-of-sync with other people working on the same database.

Impossible to keep consistent with a production database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you get to a sql console in rails?

A

rails db

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the superclass for all rails models?

A

ApplicationRecord

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is ApplicationRecord

A

The superclass that all rails models inherit from.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

When does code directly inside a class definition run?

A

When the class is first parsed.

24
Q

How do you recreate your db from scratch? What steps does this take?

A

rails db:reset

  1. drops entire database
  2. runs db:migrate
  3. runs db:seed
25
Q

What arguments are passed to has_many? How many of these are optional?

A

has_many :relation_name,
class_name: __,
foreign_key: __,
primary_key: __

All but the relation_name are optional

26
Q

What’s the difference between has_one and has_many?

A

has_one is just a has_many with a call to Array#first on the result

27
Q

How does a “has_many through” association know which association to go through?

What does this do in SQL?

A

through: :which_association

joins the model’s table to the through: table

28
Q

How do you search for files in Atom?

A

cmd + T

29
Q

How do you center a div?

A

margin 0 auto

30
Q

Why do inline-block elements have spacing between them?

How do you fix this?

A

They’re treated like words. The “\n” after a tag is interpreted as whitespace.

To fix: float one element to align to the other, or set text-size: 0

31
Q

SQL: How do you specify an INTEGER to auto-increment?

A

SERIAL

32
Q

How do you list all dbs via psql?

A

\l (lowercase L)

33
Q

How do you escape quotes in SQL

A

double the quote

34
Q

How do you do emojis in Atom

A

cmd + ctrl + space

35
Q

What’s the default join type?

A

INNER JOIN

36
Q

How do you validate that a model have a unique name?

A

validate :name, uniqueness: true

37
Q

What will this do:

validate :name, uniqueness: true

A

Validate that a model has a unique name

38
Q

How do you validate that a model contain a particular attribute?

A

validate :attribute_name, presence: true

39
Q

What will this do:

validate :attribute_name, presence: true

A

validate that a model contains a particular attribute

40
Q

How do you create an optional belongs_to association?

A

pass in this parameter to belongs_to

optional: true

41
Q

How do we skip a validation if an object is nil? How about if it is empty?

A

pass in this parameter to validates :

allow_nil: true

allow_blank: true

42
Q

How do you optionally specify a validation?

A

pass in this parameter to validates :

if: :my_boolean_method?

43
Q

What’s the major difference between an ActiveRecord::Relation and an array?

A

The former is lazy. The contents are not fetched until needed.

44
Q

What are two ways of forcing the evaluation of a Relation?

A

to_a

Relation#load

45
Q

In relation to queries within ActiveRecord::Base, what is a scope?

Why is it called a scope?

A

An ActiveRecord::Base class method that returns all or part of a query and then returns the resulting Relation object

It’s called a scope because we can tack more query method onto the end of it. It effectively “scopes” the space in which we’re querying data from.

46
Q

Can we use a scope from an instance? Why or why isn’t this surprising?

A

Yes, we can.

It’s surprising because we define it in a class method but it can be used as an instance method.

47
Q

What’s the difference between ::find and ::find_by ?

A

::find accepts a single argument, the id of the record in question, and raises an exception if the record is not found.

::find_by accepts an options hash, which allows for more detailed search criteria, and returns nil if the record is not found.

48
Q

What query method can you use to retrieve records in a specific order?

A

::order

49
Q

How do you use your own SQL to locate a record?

A

::find_by_sql

50
Q

How does rails deal with the SQL ternary problem? What is the SQL ternary problem?

A

Comparing something with a NULL object always returns NULL.

rails uses the ::where method to correctly specify one of these two comparisons:

obj = ?
obj IS NULL

51
Q

How would you run a rails query for a User that is not NULL?

A

User.where.not(name: nil)

52
Q

How do you set a column to be required within a migration?

A

add this to the end of the t.coltype line:

null: false

ex:

t.text “body”, null: false

53
Q

How do you prefetch associations for a model?

A

includes(:association_name)

54
Q

includes(:association_name)

What does this code do?

A

Prefetches objects associated with an association

55
Q

What’s an N+1 query? Why is it bad? How can you get around it?

A

It means you run N additional queries when fetching multiple objects from the same relation.

This is expensive in DB overhead.

You can get around it by prefetching data from the association using #includes

56
Q

CSS: What is a clearfix?

A

An element that has the following properties for the purpose of containing a float (using the dimensions of the float object to dictate the dimensions of its parent):

.clearfix:after {
content: "";
display: block;
clear: both;
}
57
Q

How do you debug queries generated by rails?

A

add .to_sql on the end of a query