Ruby on Rails Flashcards
WHAT IS THE PURPOSE OF YIELD?
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
DEFINE THE RAILS MVC IMPLEMENTATION USING AN EXAMPLE.
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.
WHAT IS SCOPE?
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.
CAN YOU GIVE AN EXAMPLE OF A CLASS THAT SHOULD BE INSIDE THE LIB FOLDER?
Modules are often placed in the lib folder.
WHAT DEPLOYMENT TOOL DO YOU USE?
heroku
HOW CAN YOU MIGRATE YOUR DATABASE SCHEMA ONE LEVEL DOWN?
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
WHAT IS A FILTER? WHEN IT IS CALLED?
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
WHAT DO CONTROLLERS DO IN RAILS?
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 CAN YOU LIST ALL ROUTES FOR AN APPLICATION?
rake routes – will display all routes for an application.
WHAT IS RESTFUL ROUTING?
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"
WHAT IS THE PURPOSE OF LAYOUTS?
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.
WHAT IS RAKE?
rake is command line utility of rails
For ex, Rake is most often used for DB tasks:
rake db:migrate
rake db:reset
WHAT IS THE DIFFERENCE BETWEEN HAS_ONE AND BELONGS_TO?
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.
WHAT IS A POLYMOROPHIC ASSOCIATION?
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
WHAT IS EAGER LOADING?
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