2017 week 1 Flashcards

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

What is the difference between a ‘collection route’ and a ‘member route?’

A

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.

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

What does a ‘collection route’ and a ‘member route’ have in common?

A

They’re both ways to add additional actions to a resource-based route in Rails.

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

What is ORM?

A

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.

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

What does Active Record as an ORM Framework allow us to do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How Active Record fits into the Model-View-Controller paradigm?

A

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.

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

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”

A

user.save

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

What 2 methods return false when validation fails

A

save
update

they return false when validation fails and they didn’t actually perform any operation on the database.

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

What is the schema.rb file?

A

It shows the up-to-date structure of your database.

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

What 2 columns are created by t.timestamps in the database

A

The timestamps macro adds two columns, created_at and updated_at.

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

What does the following command do?

$ bin/rails db:rollback STEP=3

A

revert the last 3 migrations.

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

What are The Golden Three Rules of using ‘self’?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Class methods vs instance methods

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you verify whether or not a particular attribute of an object is valid?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you check which validations failed on an invalid attribute?

A

errors.details[:attribute]. It returns an array of hashes with an :error key to get the symbol of the validator

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

How do you manually write the initial database migration for a Post model with attributes title:string and body:text?

A
class CreatePosts < ActiveRecord::Migration[5.0]
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body
  t.timestamps
end   end end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you show the first 120 characters of an attribute body that belongs to a model Post in the view?

A

< % = post.body.truncate(123) % >

17
Q

Write the destroy action in a controller for posts.

A
def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to posts_path
  end
18
Q

What does Bundler do?

A

Bundler makes sure that Ruby can find all of the gems (along their dependencies) listed in the Gemfile

19
Q

How do you initially create a Ruby gem in the command line?

A

bundle gem your_gem_name

20
Q

What is .gitignore?

A

This file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected

21
Q

What is a .gemspec file?

A

This file provides metadata about the gem, such as the name, description, author, license, and any gem dependencies required for it to work. It also provides path information that specifies what files to include when packaging, as well as updating the load path to include this directory when the gem is first loaded so that absolute paths are not needed when requiring any of your gem’s files.

22
Q

What does %Q and %q do in Ruby?

e. g. %Q{hello john }
e. g. %q{hello john}

A
%Q This is an alternative for double-quoted strings, when you have more quote characters in a string
e.g. 'hello john'
%q
Used for single-quoted strings.
e.g. 'hello john'
23
Q

What does %r do in Ruby?

A

%r
Used for regular expressions.The syntax is similar to %Q.

> > %r(/home/#{foo})
=> “/\/home\/Foo/”

24
Q

What does %s do in Ruby?

A

%s
Used for symbols.It’s not subject to expression substitution or escape sequences.

> > %s(foo)
=> :foo

> > %s(foo bar)
=> :”foo bar”

> > %s(#{foo} bar)
=> :”#{foo} bar”

25
Q

What is a development dependency?

A

Something your gem needs only for development or testing

26
Q

What is a runtime dependency?

A

Something that is required by your gem at runtime. When your gem is included in a third party application, it will tell Bundler to also install all runtime dependencies, while all development dependencies are ignored.

27
Q

In a Ruby gem, what is the purpose of version.rb?

A

This file contains nothing more than the version number encapsulated in the gem’s base module. As updates are released, it’s necessary to update the version number here to reflect the latest available version on RubyGems.org. It’s considered a best practice to follow semantic versioning. In short, what this means according to semver.org:

MAJOR version when you make incompatible API changes,
MINOR version when you add functionality in a backwards-compatible manner, and
PATCH version when you make backwards-compatible bug fixes. Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

28
Q

For a Ruby gem called dogeify, what does the file dogeify.rb do?

A

This is the file that is first loaded by default when your gem is required by Bundler. It initially contains nothing more than a reference to the version.rb file along with an empty module definition. As you begin coding, this file will either grow in terms of code or grow in terms of requires (or both).

For smaller gems (e.g.: a few simple methods), you might end up adding all your code to this file. For larger gems (e.g.: difficult verbose processing; multiple internal or external classes), you’ll likely want to separate your code into multiple files, requiring them from this file similar to how version.rb is required. It’s also important to note that despite our gem being defined as a module, you’re free to change this to a class. Note though that changing this from a module to a class in this file also requires other files referencing Dogeify to be updated as well.