Chapter 2 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a scaffold generator?

A

Part of Rails which generates code and thereby functionality

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

How can we generate a new Rails app using a specific version of Rails?

A

$ rails railsversionnum new appName

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

What is a data model?

A

A representation of the structures needed by our application

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

What is one way to approach creating a data model?

A

Identify WHO and WHAT will be needed in the app (ex. users & microblogs), and break down the data/data types needed for each object.

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

Explain the following command:

’$ rails generate scaffold User name:string email:string

A

This generates a scaffold by passing the ‘scaffold’ command to the ‘rails generate’ script.

The argument of the scaffold command is the singular version of the resource name (in this case, User), together with optional parameters for the data model’s attributes (where the syntax is dataName:dataType)

Note that there is no need to include a parameter for id; it is created automatically by Rails for use as the primary key in the database.

Lastly, note that ‘User’ is SINGULAR! Rails will make it plural by adding an ‘s’ where necessary.

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

Why bother adding ‘bundle exec’ to the following command?

’$ bundle exec rake db:migrate’

A

‘bundle exec’ ensures that the Rake version specified in our Gemfile is used

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

After creating an object through scaffolding, what is the next step?

A

Update our database with the new model via a database migration.

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

Having made a model through scaffolding, how can we access it in our browser?

A

from the root URL, go to /modelName to see the contents of the database and a dialog to edit it.

/modelName/1 - go to user with ID of 1

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

Articulate the process of a typical browser hit to the index page /users using the MVC model

A
  1. The browser issues a request for the /users URL.
  2. Rails routes /users to the index action in the Users controller.
  3. The index action asks the User model to retrieve all users (User.all).
  4. The User model pulls all the users from the database.
  5. The User model returns the list of users to the controller.
  6. The controller captures the users in the @users variable, which is passed to the index view.
  7. The view uses embedded Ruby to render the page as HTML.
  8. The controller passes the HTML back to the browser.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the following do in the routes.rb file?

resources :users

A

Maps the /users URL to the controller actions for the ‘Users’ resource. This tells the Rails router, which receives a request from a browser, where to route the request to the proper controller action based on the URL

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

What is the definition of a controller in Rails?

A

A collection of related actions (functions)

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

What is the REST architecture?

A

The ideas of representational state transfer as identified and named by computer scientist Roy Fielding for developing distributed, networked systems and software applications such as the World Wide Web and web applications.

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

Explain REST in the context of Rails.

A

Most application components (such as users and microposts) are modeled as resources that can be created, read, updated, and deleted—operations that correspond both to the CRUD (create, read, update, delete) operations of relational databases and to the four fundamental HTTP request methods: POST, GET, PATCH, and DELETE.

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

Explain a RESTful style of development in Rails

A

The RESTful style of development helps you make choices about which controllers and actions to write: you simply structure the application using resources that get created, read, updated, and deleted.

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

A model file in apps/models generated by scaffolding appears with little to no content; how does it get its functionality?

A

Via inheritance from ActiveRecord::Base

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

In the ‘users’ controller, what does ‘@users = User.all’ do?

A

Tells the ‘User’ model to retrieve a list of all the users from the database and place them in the instance variable ‘@users’

17
Q

When an instance variable is assigned to a controller object, where is that instance variable made available?

A

In the corresponding view, in app/views/controllerName/index.html.erb

18
Q

T/F: scaffolding updates the Rails routes to reflect the new resource.

A

T; it maps the new resource URL to the resource’s controller

19
Q

Where is the database that we must migrate to, and what is it?

A

It is a SQLlite database located in /db

20
Q

What is a validation, and where is it defined?

A

A validation will send an error message if the established criteria are not met. They are stored in the model files in apps/models

The following will cause an error message to be given and the database will not be updated if the length of a new micropost is > 140 characters:

class Micropost < ActiveRecord::Base
  validates :content, length: { maximum: 140 }
end
21
Q

How can an association be defined?

A

In one model, add ‘has_many :associatedModel’.

In the model to be associated, add ‘belongs_to :ownerModel’

22
Q

T/F: Rails can infer the values to be associated between two models

A

T; Using the (implicit) ID of one model, Rails can infer that an integer value such as ‘user_id’ is the value to be associated.

23
Q

How can the Rails console be entered and exited?

A

$ rails console

> > > exit

24
Q

What is the Rails console?

A

A REPL for interacting with Rails applications (works similar to IRB)

25
Q

What is the inheritance structure for models?

A

They all inherit from ActiveRecord::Base

26
Q

What is the inheritance structure for controllers?

A

Controllers inherit from the ApplicationController class (defined in app/controllers/application_controller.rb), which in turn inherits from ActionController::Base

27
Q

Why is the following command necessary after initially deploying to Heroku when the app contains a database?

$ heroku run rake db:migrate

A

Heroku’s PostgreSQL database back-end needs to be updated with the data models in order to work properly with the app

28
Q

What can be added to a model to make sure that a field is not left blank?

A

add ‘validates :fieldName, presence: true’