Ruby on Rails Flashcards

1
Q

Gem Specification:

gem ‘rails’, ‘4.0.0’

A

is “absolute”, only version 4.0.0 will be used

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

Gem Specification:

gem ‘rails’, ‘>= 4.0.0’

A

is “optimistic”, any version newer than 4.0.0 will be used

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

Gem Specification:

gem ‘rails’, ‘~> 4.0.0’

A

“pessimistic”;

~> 4.0.0 means use
any version greater than 4.0.0 and less than 4.1 (any patch version can be used). ~> 4.0
means use any version greater than 4.0 and less than 5.0 (any minor version can be used).

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

Changes in routes file

A

When you change the config/routes.rb file, you must restart your application server to see
the new behavior.

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

Can a controller use data from more than one model?

A

. In Rails, though a controller can use data from more than one model,
there is often a model with the same name as a controller

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

How do you pass variables to the view file from the controller?

A

Any instance variables (variables named with the @

character) will be available in the corresponding view file.

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

understanding params (add more)

A

params = {controller: ‘contacts’,
action: ‘process_form’,
contact: {name: ‘Daniel’ , email: ‘daniel@danielkehoe.com@’, content: ‘hi!’}
}

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

skinny controller, fat model

A

Controllers should
contain only enough code to instantiate a model and handle rendering. If you think about it,
adding a subscriber to the MailChimp database is a data operation, and all data
manipulation should be handled by a model.

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

How do you run bundle without installing production gems?

A

bundle install –without production

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

Why do you need to precompile assets?

A

If you don’t precompile assets for production, all web pages will look strange. They won’t
have CSS styling.

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

How do instance variables help performances?

A

Setting an instance variable (in controller) caches the data so it doesn’t make a new database call each find the instance is searched for.

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

What does t.timestamps create?

A

It will create two columns inside our table titled created_at and updated_at

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

What are the 7 core actions of rails’ REST implementation?

A
index
new
create
show
update
edit
destroy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is form_for and how do you use it?

A

form_for is a Rails helper method which takes one parameter, in this case @article and a block with the form fields. The first line basically says “Create a form for the object named @article, refer to the form by the name f and add the following elements to the form…”

Example:

<li></li>

</ul>

<p>
<br></br>

</p>

<p>
<br></br>

</p>

<p>

</p>

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

How is form data accessible?

A

The data from the form will be accessible through the params method.

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

Where should flash messages be added?

A

In the layout for use across the entire application.

17
Q

What is the difference between new and create?

A

new doesn’t send the data to the database until you call save. With create you build and save to the database all in one step

18
Q

Changing a form label - in this case from Author Name to Your Name

A

<br></br>

19
Q

Note about join tables

A

Most of the time a join table isn’t the best solution, so promote the joining feature to its own model

20
Q

Rails offers four standard spots to place initialization code. Where are they?

A

config/application.rb
Environment-specific configuration files
Initializers
After-initializers

21
Q

What is bundler?

A

Bundler is a tool that manages gem dependencies for your Ruby application. The first time bundler is used is when the application is created - automatically built in.

22
Q

What gem does the application use if you don’t include a version?

A

The gem directive takes an optional second argument describing the version of the Rubygem desired. Leaving the version argument off will simply get the latest available stable version, which may not be the latest version available. To include a release candidate or a pre-release gem you’ll need to specify the version explicitly.

23
Q

Rails routing order

A

Routes are consulted, both for recognition and for generation, in the order they are defined in routes.rb. The search for a match ends when the first match is found, meaning that you have to watch out for false positives.

24
Q

Don’t use numbered IDs in URLs

A

Why shouldn’t you use numeric IDs in your URLs? First, your competitors can see just how many auctions you create. Numeric consecutive IDs also allow people to write automated spiders to steal your content. It’s a window into your database. And finally, words in URLs just look better.

25
Q

The belongs_to association supports these 9 options:

A
\:autosave
\:class_name
\:counter_cache
\:dependent
\:foreign_key
\:inverse_of
\:polymorphic
\:touch
\:validate
26
Q

:class_name option

A

If the name of the other model cannot be derived from the association name, you can use the :class_name option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is Patron, you’d set things up this way:

class Order < ActiveRecord::Base
  belongs_to :customer, class_name: "Patron"
end
27
Q

:foreign_key option for associations

A

By convention, Rails assumes that the column used to hold the foreign key on this model is the name of the association with the suffix _id added. The :foreign_key option lets you set the name of the foreign key directly:

class Order < ActiveRecord::Base
  belongs_to :customer, class_name: "Patron",
                        foreign_key: "patron_id"
end
28
Q

Does rails create foreign keys automatically?

A

In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.