Rails Basics Flashcards
What is Rails
open source web application framework written in Ruby.
Active Record Pattern
a database table is represented as a class; each row of the tables is created/updated by an object instance.
Convention over Configuration
A software design that decreases the number of decisions to be made by developers
E.g. Rails automatically render views with names that correspond to valid routes.
What is a Framework?
A framework is a program or code library that writes most of your application for the developer.
What is MVC?
Model: Application data.
View: Presentation layer
Controller: Processes and responds to events
How does MVC work?
Request first comes to the controller, controller finds the 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
Explain RESTful Architecture
REST = REpresentational State Transfer
It uses HTTP methods GET, PUT, POST, or DELETE
RESTful interface means clean URLs, less code.
Why Rails?
DRY Principal( Don’t Repeat Yourself)
Convention over Configuration
Gems
What is the ORM of rails?
ORM stands for Object-Relationship-Model, where classes are mapped to tables in the database, and objects are directly mapped to the rows in the table
What Associations Relationships does a Model have?
one-to-one: exists when one item has exactly one of another item.
one-to-many: exists when a single object can be a member of many other objects.
many-to-many: exists when the first object is related to one or more of a second object, and the second object is related to one or many of the first object.
belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many
What is the Ruby naming convention for methods?
Lowercase names that may be followed by digits, underscores, letters, e.g. paint or close_the_door
What are Instance and Class variables?
Instance variables(@): the data varies with each instance of the object. @i = 1
Class variables(@@): “static variables”, shared across all instances of a class. @@i = 1
What is the difference between empty , nil and blank?
Nil: used on any object and is true if the object is nil
Empty: can be used on strings, arrays and hashes and returns true if:
String length == 0;
Array length == 0;
Hash length == 0;
.blank? evaluates true on strings which are non-empty but contain only whitespace.
What is a destructive method?
It is a method that alters the attribute of an object.
Example:
myabcs = [‘a’, ‘b’, ‘c’] #myabcs is of type Array
myabcs.reverse! #this method altered the object
p myabcs #outputs
#[[“c”,”b”,”a”]
What are the variable scopes?
Local variables are local to the code construct in which they are declared. eg: method, loop.
must begin with either an underscore or a lower case letter
i = 5
Global variables accessible from anywhere in the Ruby program.
$speed = 10
Class variable is shared amongst all instances of a class. Only one variable value exists for all objects. If one object instance changes the value of the variable, that new value will change for all other object instances.
@@i = 5
Instance variables are local to specific instances of an object.
@i = 5
Ruby constants are values which, once assigned a value, should not be changed.
GRAVITATIONAL_CONST = 6.67