Ruby on Rails Flashcards

1
Q

WHAT IS THE PURPOSE OF YIELD?

A

The interpreter essentially invokes a separate piece of code and places it in the location (of the YIELD). Ruby’s yield statement gives control to a user specified block from the method’s body

For example, you may want a common header and footer on all pages of your app. You can place a Yield in the body and custom code from another file can be placed there.

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers

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

DEFINE THE RAILS MVC IMPLEMENTATION USING AN EXAMPLE.

A

a) Model: Maintains the relationship between Object and Database and handles validation, association, transactions. Each model (can) represent a database table. This model object gains capabilities (inherited from ActiveRecord — Rails class) to retrieve, save, edit, and delete data from database table. We use model objects as a layer between our application and the database.
b) Controller – The facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organizing that data (searching, sorting) into a form that fits the needs of a given view. It takes care of the flow: uses models to do queries, parses data, and make decisions about in which format you’ll present the data.
c) View: A presentation of data in a particular format, triggered by a controller’s decision to present the data.
This is the presentation of the request’s response. This presentation can be in a bunch of format types: PDF, HTML, JSON, etc.

User uses controller, which manipulates model, which updates view for user to see

Request first goes to the controller, controller finds an appropriate view and interacts with model, model interacts with your database and send the response to controller then controller, based on the response, give the output parameter to view.

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

WHAT IS SCOPE?

A

Scopes are nothing more than SQL scope fragments. By using these fragments one can cut down on having to write long queries each time you access content.

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

CAN YOU GIVE AN EXAMPLE OF A CLASS THAT SHOULD BE INSIDE THE LIB FOLDER?

A

Modules are often placed in the lib folder.

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

WHAT DEPLOYMENT TOOL DO YOU USE?

A

heroku

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

HOW CAN YOU MIGRATE YOUR DATABASE SCHEMA ONE LEVEL DOWN?

A

It has this nifty syntax to go back one step:
rake db:rollback

If you want to rollback multiple steps at the same time you would use:
rake db:rollback STEP=3

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

WHAT IS A FILTER? WHEN IT IS CALLED?

A

Filters are methods that are run “before”, “after” or “around” a controller action.

Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.

Ex: before_action :require_login

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

WHAT DO CONTROLLERS DO IN RAILS?

A

Once a request comes into the Rails stack, it goes to the routes table to determine which controller and action should be called.

Once a controller action is determined the request is routed to the controller and it does the needed processing by connecting with the DB if needed and then it sends control to the View to render the output.

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

HOW CAN YOU LIST ALL ROUTES FOR AN APPLICATION?

A

rake routes – will display all routes for an application.

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

WHAT IS RESTFUL ROUTING?

A

Instead of relying exclusively on the URL to indicate what webpage you want to go to (and just using the one method), it’s a combination of method and URL.

This way, the same URL, when used with a different verb (such as GET, PUT, POST, DELETE), will get you to a different page. This makes for cleaner, shorter URLs, and is particularly adapted to CRUD applications.

/users/       method="GET"
/users/1      method="GET"
/users/new    method="GET"
/users/       method="POST"
/users/1/edit method="GET"
/users/1      method="PUT"
/users/1      method="DELETE"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

WHAT IS THE PURPOSE OF LAYOUTS?

A

Layouts are partial ruby/html files that are used to render the content pages.

There are placed in the folder: app/views/layouts

Items that you would typically put in this folder are things like headers/footers, navigation elements, etc.

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

WHAT IS RAKE?

A

rake is command line utility of rails

For ex, Rake is most often used for DB tasks:
rake db:migrate
rake db:reset

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

WHAT IS THE DIFFERENCE BETWEEN HAS_ONE AND BELONGS_TO?

A

A has_one relationship is used to define a 1:1 relationship between two objects.

Examples are:
A PROJECT has_one PROJECTMANAGER

A belongs_to relationship on the other hand is used to define the reverse association for the same 1:1 relationship that is defined using the has_one keyword.

The important thing to keep in mind is that you need to declare both associations in order for the relationships to work correctly.

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

WHAT IS A POLYMOROPHIC ASSOCIATION?

A

With polymorphic associations, a model can belong to more than one other model, on a single association.

For example, you have Course and Lab models in your application. Both need Teaching Assistants, so you need to associate Course and Labs to their corresponding TAs. If you use has_many/belongs_to kind of association, you will have two similar models for TAs of course and TAs of Lab. Instead of having two different models you can have a single model i.e TeachingAssistant and you can associate this model with Course and Lab models using polymorphic association.

https://launchschool.com/blog/understanding-polymorphic-associations-in-rails

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

WHAT IS EAGER LOADING?

A

Eager loading is a great optimization strategy to reduce the number of queries that are made against the DB. You load data from related entities all at once when it’s too costly to load with each query.

Say you are finding 10 employees and then you are looking for their post codes. Then your query would appear something like this:

clients = Client.limit(10)
clients.each do |client|
  puts client.address.postcode
end
This may seem fine at first look but really this implementation leaves much to be desired. It makes 11 DB calls just to get the results.

Now you can optimize this query by making a slight change in the request like this:

clients = Client.includes(:address).limit(10)
clients.each do |client|
puts client.address.postcode
end

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

What is an N + 1 query?

A

Say you are finding 10 employees and then you are looking for their zip codes. Then your query would appear something like this:

clients = Client.limit(10)
clients.each do |client|
puts client.address.zip
end

It makes 11 DB calls just to get the results (1 query for loading the clients, and N queries for loading the address in each iteration, hence the name N+1 queries)

17
Q

HOW DOES VALIDATION WORK?

A

Validation means checking to see if data is good before it is stored in the database.

During signups and other such user input cases you want to check and be sure that the data is validated. In the past developers would often put this type of validation logic as triggers in the database.

In an MVC architecture one can do validations at each level.

You can do validations in the controllers but it is usually a good idea to keep your controllers skinny.

Views suffer from the javascript limitation because javascript can be disabled on the client side so they are not completely reliable.

The best way to manage validation is to put it in the model code. This model code is really the closest as you can be to the database and works very well for Rails applications.

Ex:
class Person < ActiveRecord::Base
  validates :name, :length => { :minimum => 2 }
18
Q

HOW CAN YOU INSTALL THE MISSING GEMS THAT ARE REQUIRED BY YOUR APPLICATION IN THE SIMPLEST WAY?

A

bundle install
This command will install all dependencies and missing gems. It looks at your Gemfile to figure out what gems are needed by your application.

19
Q

HOW CAN YOU SAFEGUARD A RAILS APPLICATION FROM SQL INJECTION ATTACK?

A

Rails already has the logic built into it to prevent SQL injection attacks if you follow the right syntax.

Say you are trying to authenticate a user based on their login and password. To prevent SQL injection attack, use following format:

User.where(“login = ? AND password = ?”, entered_user_name, entered_password).first

20
Q

In which Programming language was Ruby written?

A

Ruby was written in C language and Ruby on Rails written in Ruby.

21
Q

Why Ruby on Rails? / What do you like about Ruby on Rails?

A

I researched that alot of web development frameworks make you write pages of configuration code. With Rails, as long as you follow the suggested naming conventions, it doesn’t require spending much time on configuration and you can almost go straight to developing your app.

22
Q

How many types of relationships does a Model have?

A

(i) has_one
(ii) belongs_to
(iii) has_many
(iv) has_many :through

23
Q

What is Session and Cookies?

A

Session is used to store user information on the server side where as Cookies are used to store the information in the client side.

http://technoscrum.blogspot.com/2013/07/ruby-on-rails-interview-questions.html

24
Q

What is the basic difference between GET and POST method?

A

Data can be sent through the request body of a POST request. GET is basically for just retrieving data, whereas POST may used to do multiple things, like storing or updating data.

25
Q

What is ORM?

A

ORM stands for Object-Relational-Mapping, it means that your Classes are mapped to table in the database and Objects are directly mapped to the rows in the table.

26
Q

What is Bundler?

A

It helps you manage your gems for the application. After specifying gems in your Gemfile, you need to do a bundle install. If the gem is available in the system, bundle will use that, else it will pick up from the rubygems.org

27
Q

What is the difference between Render & Redirect?

A
  • render is a method used to create the content.
  • Redirect is used to tell the browser to issue a new request.
  • Redirect is used when the user needs to redirect its response to some other page or URL. Whereas, render method renders a page and generate a code of 200.
28
Q

What is Gemfile and Gemfile.lock?

A

The Gemfile is where you specify which gems you want to use, and lets you specify which versions. The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and installing the most recent versions. (Running different versions on different machines could lead to broken tests, etc.) You shouldn’t ever have to directly edit the lock file.

29
Q

What is has_many ?

A

A has_many association indicates a one-to-many connection with another model. You’ll often find this association on the “other side” of a belongs_to association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, a customer has_many orders and an order belongs_to a customer.

30
Q

Explain the Naming Convention in Rails.

A
Variables: Variables are named where all letters are lowercase and words are separated by underscores. E.g: total, order_amount. 
Class and Module: Classes and modules uses MixedCase and have no underscores, each word starts with a uppercase letter. Eg: InvoiceItem
Database Table: Table name have all lowercase letters and underscores between words, also all table names to be plural. Eg: invoice_items, orders etc
Model: The model is named using the class naming convention of unbroken MixedCase and always the singular of the table name.
For ex: table name might be orders, the model name would be Order. Rails will then look for the class definition in a file called order.rb in /app/model directory. If the model class name has multiple capitalized words, the table name is assumed to have underscores between these words.
Controller: controller  class names are pluralized, such that OrdersController would be the controller class for the orders table. Rails will then look for the class definition in a file called orders_controlles.rb in the /app/controller directory.
31
Q

What is Active Record?

A

ActiveRecord is a Ruby library for working with Relational SQL Databases like Postgres. It provides an Object Relational Mapping (ORM) with these core features:
- a single Ruby object maps to a database table
columns are accessed by methods, and are inferred from the database schema
- methods for create, read, update, and delete (CRUD) are defined.
- a DSL for easily constructing SQL queries in Ruby