w3d3_notes Flashcards

1
Q

What does Singleton enforce? Why does this matter for databases?

A

Singleton enforces a single instance of a class (interact with it via ‘instance’). For a class with a db connection, this means that only one connection to the database is maintained.

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

How can an SQL injection be achieved?

A

By inserting a string with semicolons:

“blahblahblah; DROP TABLE;”

If the characters (namely the semicolons) are not escaped, then the malicious code will be executed.

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

When should you NOT use the hash declaration shortcut syntax:

{id: ‘id’, val: 0}

A

When you do NOT want your keys to be symbols.

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

T/F: An attr_accessor is fine for all column values.

A

F; :id should be an attr_reader b/c you want it to be immutable. Not that Rails does NOT do this.

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

Are N+1 queries good?

A

NO, THEY ARE EVIL! Multiple database queries are expensive (imagine trying to repeatedly query a server halfway across the world: the lag will be VERY noticeable!)

If SQL can take care of it, have SQL do it!

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

What is the SQL algorithm to retrieve all users form a db?

A

SELECT
COUNT(*)
FROM
users

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

Why should you start with the where clause?

A

If you do not specify what you want, you run the risk of accessing all records in the db (the default). This means that, for example, an update statement for one user’s password where you forget the WHERE clause will change everyone’s password!

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

What is the command to generate a migration?

A

$rails generate migration migration_name

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

What is the difference between ::create and ::create!

A

Both versions create a new instance of the class and insert it into the db. Both return true when they succeed, however on failure the bang version will raise an exception whereas the safe version will return false (a “quiet failure”

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

What is the command to reload the rails console?

A

reload!

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

What must migrations include in the create table iterations?

A

t.timestamps

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

What is a good way to think about foreign keys?

A

A king has many subjects, subjects belong to the king.

In this example, the king has the primary key :id, whereas the subjects have the foreign key :king_id

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

What should you do when you have a bad db connection in rails?

A

Check database.yml to make sure that the username (for the dev environment) is the name of the local user.

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

What is the difference between ::find_by and ::where in ActiveRecord?

A

find_by will return a single instance, whereas where will return an array of instances, even if there is only one match.

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

T/F: You should always declare associations in both directions.

A

T

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

When should you use a join table model?

A

When you have a many-to-many relationship.

17
Q

If you have chained method calls, how should you space them?

A

Put each call on an indented newline:

def method
  m1
    .m2
    .m3
    .m4
end
18
Q

What should join tables index?

A

The foreign keys that will be used.

19
Q

How do indices work?

A

Under the hood, they sort a table to make retrieving something easy.

Consider the following example:

We add an index for a foreign key :user_id. Now, when we need to retrieve a user_id from the db, the db knows to sort by user_id (by default the db is sorted by the primary key :id), then run a binary search to retrieve the value in question quickly (binary search only works on sorted lists).

Do not over-use indices; the constant need to sort can cause a massive overhead. Save indices for things you will need to query frequently.

20
Q

How can we create a CLI for a rails app?

A

Make the file bin/cli (no file extension), then fill it with a ruby script. The CLI can be run with $rails runner bin/cli