w3d6-w3d7 Flashcards

1
Q

What is a callback?

A

Code that is run at a certain moment of an object’s life cycle (i.e. at creation, updating, deletion, etc.)

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

In an association, what happens to objects that belong to another object when the object they belong to is destroyed (read: the object the foreign key points to is destroyed)?

A

The objects are considered widowed.

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

Explain the following code:

class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
end

A

When a User is destroyed, the posts that belong to it are destroyed as well.

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

Where is :dependent added?

A

In has_many relations.

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

What are some other options for :dependent?

A

:nullify

Sets the foreign key to null when its target is destroyed.

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

How do we guarantee referential integrity?

A

Add a FOREIGN KEY constraint at the db level.

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

Explain the following code:

class User  true
  before_validation :ensure_random_code
  protected
  def ensure_random_code
    # assign a random code
    self.random_code ||= SecureRandom.hex(8)
  end
end
A

This is an example of callback registration: the random code attribute is added in case it is forgotten as part of the object’s validation.

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

What is an important property of callback methods?

A

They should be protected or private.

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

What are some useful ActiveRecord callbacks?

A

before_validation (handy as a last chance to set forgotten fields upon creation, not saving/updating)
• after_create (handy to do some post-create logic, like send a confirmation email)
• after_destroy (handy to perform post-destroy clean-up logic)

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

Explain the following code:

before_validation(:on => :create) do
self.number = number.gsub(/[^0-9]/, “”) if attribute_present?(“number”)
end

A

Strip everything but digits, so the user can specify “555 234 34” or
“5552-3434” or both will mean “55523434”

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

What is wrong with the following ERB:

A

The Law of Demeter is being violated; objects are talking to objects besides their neighbors/more than one dot is being used.

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

What does #delegate do and what options does it take?

A

delegate allows getter methods to be generated for other classes in associations, so that multiple dots are no longer needed. prefix: true can be specified so that sensible prefixes can be given to these getter methods.

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

Assuming we have an office, doctor, and patient, what code would make the following ERB viable:

A
class Office < ActiveRecord::Base
  belongs_to :doctor
end
class Doctor < ActiveRecord::Base
  has_one :office
  has_many :patients
  delegate :number,
           :address,
           to: :office,
           prefix: true
end
class Patient < ActiveRecord::Base
  belongs_to :doctor
  delegate :name, 
           :specialty,
           :office_number, 
           :office_address, 
           to: :doctor, 
           prefix: true
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T/F: It does not matter what kind of SQL db you’re using; Rails migrations are db agnostic and will work with all of them.

A

T

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

What is the first thing that happens when a server running Rails gets a request from a web browser?

A

It first goes to the Rails router.

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

What does the Rails router do?

A

The router decides what the request is trying to do based on its address and other HTTP parameters.

17
Q

What is the command to see all available routes in a Rails app?

A

rake routes

18
Q

Explain the output of rake routes:

Prefix Verb URI Pattern Controller#Actionarticles GET /articles(.:format) articles#index

A

The prefix, articles, provides articles_path (the relative path) and articles_url (the full http address) as Rails methods.

The verb, GET, is the HTTP verb.

The URI pattern is like a regex in that is specifies what is given to the controller with that name (articles can take /articles/, /articles.json, etc.)

The final column is where the route maps to the controller method.

19
Q

What is special about instance variables in Rails controllers?

A

They are automatically transferred from the controller to the object which renders the view template.

20
Q

What are the arguments given to the #link_to helper?

A

The displayed text of the link, and the URL of the link (typically _path or _url macro)

21
Q

What is a synonym for a controller action?

A

A controller method.

22
Q

What action is typically used to render a view?

A

show

23
Q

Where are URL parameters stored?

A

In the ‘params’ hash

24
Q

What is the basic reason to add forms to a page?

A

To create a viable method for users to interact with the web and, by proxy, the db.

25
Q

What does #form_for do?

A

This Rails helper takes an iv param, an object, and that object can be put in a do loop for each element the user needs to input.

26
Q

Views can reference instance variables. Where are these instance variables declared?

A

In the corresponding controller method.

27
Q

What is one way to inspect the params hash?

A

Put a fail/raise statement into the controller action which is called ath the point where you want to inspect.

28
Q

What is the point of having strong parameters?

A

They prevent us from blindly accepting the entirety of the params hash as a parameter to creating an object. What if it included malicious code?

29
Q

How can we use #require to to enforce strong parameters?

A

Use it in a helper to declare, along with #permit, which parts of the params hash you want to use:

def article_params
params.require(:article).permit(:title, :body)
end

30
Q

What is the naming convention for partials?

A

Begin the filename with an underscore:

app/views/articles/_form.html.erb

31
Q

What is a flash and how can we add one to an action?

A

A flash is a message to let a user know that something has taken place:

flash.notice = “Article ‘#{@article.title}’ Updated!”