Rails Basics Flashcards
Where should you keep all of your model calls?
In your controller. This keeps your views from containing lots of ruby code
Standard Controller actions
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
before_filter :method, :only =>[methods to call it with]
tells the controller to call a method before all the actions listed in the :only key value pair.
model
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
view
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
controller
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
RESTful
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.
Active Record
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.
ORM
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 do you create Active Record Models?
All you have to do is to subclass the ActiveRecord::Base class and you’re good to go:
class Product < ActiveRecord::Base end
Active Record Migration
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
Validations
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
What are Polymorphic Associations
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
Scope
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 many Types of Associations Relationships does a Model have?
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.