Rails Basics Flashcards

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

Where should you keep all of your model calls?

A

In your controller. This keeps your views from containing lots of ruby code

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

Standard Controller actions

A

Def index, list all things

def show, show a thing

def new, show a new thing form

def create, create a new thing

def edit, show an edit thing form

def update, update a thing

def destroy, delete a thing

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

before_filter :method, :only =>[methods to call it with]

A

tells the controller to call a method before all the actions listed in the :only key value pair.

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

model

A

handels data

Models are Ruby classes. They talk to the database, store and validate data, perform the business logic and otherwise do the heavy lifting. They’re the chubby guy in the back room crunching the numbers. In this case, the model retrieves video 15 from the database.

you should place model calls in your controllers
Active Record is the model in the rails MVC

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

view

A

handles presentation

Views are what the user sees: HTML, CSS, XML, Javascript, JSON. They’re the sales rep putting up flyers and collecting surveys, at the manager’s direction. Views are merely puppets reading what the controller gives them. They don’t know what happens in the back room. In our example, the controller gives video 15 to the “show” view. The show view generates the HTML: divs, tables, text, descriptions, footers, etc.

ActionView is the view in the rails MVC

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

controller

A

handles decisions

best practice: one concern per controller

Controllers do the work of parsing user requests, data submissions, cookies, sessions and the “browser stuff”. They’re the pointy-haired manager that orders employees around. The best controller is Dilbert-esque: It gives orders without knowing (or caring) how it gets done. In our case, the show method in the video controller knows it needs to lookup a video. It asks the model to get video 15, and will eventually display it to the user.

middle man between models and views It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.

ActionController is the controller in the rails MVC

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

RESTful

A

RESTful: REST stands for Representational State Transfer. REST is an architecture for designing both web applications and application programming interfaces (API’s), that’s uses HTTP.

RESTful interface means clean URLs, less code, CRUD interface. CRUD means Create-READ-UPDATE-DESTROY. In REST, they add 2 new verbs, i.e, PUT, DELETE.

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

Active Record

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.
like Object Relational Mapping (ORM), where classes are mapped to table, objects are mapped to columns and object attributes are mapped to data in the table.

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

ORM

A

Object Relational Mapping 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
10
Q

How do you create Active Record Models?

A

All you have to do is to subclass the ActiveRecord::Base class and you’re good to go:

class Product < ActiveRecord::Base
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Active Record Migration

A

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don’t have to write SQL by hand, allowing your schema and changes to be database independent.

You can think of each migration as being a new ‘version’ of the database

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.text :description
  t.timestamps
end   end end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Validations

A

Validations are used to ensure that only valid data is saved into your database. For example, it may be important to your application to ensure that every user provides a valid email address and mailing address. Model-level validations are the best way to ensure that only valid data is saved into your database. They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain. Rails makes them easy to use, provides built-in helpers for common needs, and allows you to create your own validation methods as well.

class Person < ActiveRecord::Base
  validates :name, presence: true
end

Person.create(name: “John Doe”).valid? # => true
Person.create(name: nil).valid? # => false

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

What are Polymorphic Associations

A

A slightly more advanced twist on associations is the polymorphic association. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here’s how this could be declared:

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
end

class Employee < ActiveRecord::Base
has_many :pictures, as: :imageable
end

class Product < ActiveRecord::Base
has_many :pictures, as: :imageable
end

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

Scope

A

Active Record Query Interface

write name scopes in you models

Active Record insulates you from the need to use SQL in most cases, and provides you with finder methods that you can pass arguments into to perform certain queries on your db without writing raw SQL

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

How many Types of Associations Relationships does a Model have?

A

When you have more than one model in your rails application, you would need to create connection between those models. You can do this via associations. Active Record supports three types of associations:

one-to-one: A one-to-one relationship exists when one item has exactly one of another item. For example, a person has exactly one birthday or a dog has exactly one owner.

one-to-many: A one-to-many relationship exists when a single object can be a member of many other objects. For instance, one subject can have many books.

many-to-many: A many-to-many relationship 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.

You indicate these associations by adding declarations to your models: has_one, has_many, belongs_to, and has_and_belongs_to_many.

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

What are helpers and how to use helpers in ROR?

A

Helpers are modules that provide methods which are automatically usable in your view. They provide shortcuts to commonly used display code and a way for you to keep the programming out of your views. The purpose of a helper is to simplify the view.

17
Q

What things we can define in the model?

A
  1. Validations (like validates_presence_of, numeracility_of, format_of etc.)
  2. Relationships (like has_one, has_many, HABTM etc.)
  3. Callbacks (like before_save, after_save, before_create etc.)
  4. ROR Queries in Sql
  5. Active record Associations Relationship