13. Models ➤ Associations
# create a product and find its manufacturer
product = manufacturer.products.create({:name => "Kitlifter"})
product.manufacturer
# create a upc and find its product's manufacturer
upc = product.create_upc({:code => "001122"})
upc.product.manufacturer
# create a batch (linking a product and ingredient)
wheat = Ingredient.create({:name => "Wheat"})
bread = Product.create({:name => "Bread"})
batch.create({:product => bread, :ingredient => wheat})
Saturday, April 2, 2011
14. Models ➤ Associations
When should I use has_one
and belongs_to?
“Using has_many or belongs_to is more than
just on which table the foreign key is placed,
itʼs a matter of who can be thought of as
ʻowningʼ the other. A product ʻownsʼ a UPC.”
Saturday, April 2, 2011
15. Models ➤ Validations
class Product < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
validates_format_of :sku, :with => /^SKUd{8}$/
validates_inclusion_of :usda_rating, :in => %w( prime choice )
validate :cannot_be_active_if_recalled
def cannot_be_active_if_recalled
if recalled? && recalled_on < Date.today
errors.add(:active, "Can't be active if it's been recalled")
end
end
Saturday, April 2, 2011
16. Models ➤ Validations
I know my record is
technically invalid, but
I want to save it anyhow.
“It is possible to save a record,
without validating, by using
save(:validate => false)”
Saturday, April 2, 2011
17. Models ➤ Callbacks
class Ingredient
Callback
before_destroy :determine_destroyability Chain
before_create :format_legacy_name
after_update :log_changes
after_create :import_harvest_data
determine_destroyability
# validation
# create
# save
# update
# destroy
end
STOP!!
Saturday, April 2, 2011
18. Models ➤ Querying
Product.find(98)
Product.find_by_name("Diet Coke")
Product.find_by_name_and_sku("Diet Coke", "SKU44387")
Product.find([98,11,39])
Product.first
Product.last
Product.all
Product.count
# old and busted
Product.find(:all, :conditions => {:name => "Cheese-it!"})
# new hotness
Product.where(:name => "Cheese-it!").all
Saturday, April 2, 2011
21. Models ➤ Validations
Why would I use joins
instead of includes?
“Using includes will load the
records into memory when the query
is executing, joins will not.”
Saturday, April 2, 2011
22. Controllers
Routing
Filters
Conventions
Saturday, April 2, 2011
23. Controllers ➤ Routing
resources :products
# GET /products => index action
# GET /products/new => new action
# GET /products/:id => show action
# GET /products/:id/edit => edit action
#
# POST /products => create action
#
# PUT /products/:id => update action
#
# DELETE /products/:id => destroy action
products_path # /products
products_url # http://www.example.com/products
product_path(@product) # /products/29
product_path(@product, :xml) # /products/29.xml
Saturday, April 2, 2011
24. Controllers ➤ Routing
namespace :admin do
resources :users
resources :orders
end
admin_users_path # /admin/users
edit_admin_order_path # /admin/orders/4/edit
class Admin::UsersController < ApplicationController
# /app/controllers/admin/users_controller.rb
# /app/views/admin/users/
end
Saturday, April 2, 2011
25. Controllers ➤ Routing
resources :accounts, :except => :destroy do
resources :users do
post :activate, :on => :member
collection do
get 'newest'
end
end
resources :clients, :only => [:index, :show]
end
account_users_path(@account) # /accounts/182/users
newest_account_users_path(@account) # /accounts/182/users/newest
activate_account_user_path(@account, @user) # /accounts/182/user/941
accounts_clients_path(@account) # /accounts/182/clients
new_accounts_client_path(@account) # FAIL!
Saturday, April 2, 2011
26. Controllers ➤ Filters
class UsersController < ApplicationController
before_filter :load_manufacturer
before_filter :find_geo_data, :only => [:show]
skip_before_filter :require_login
after_filter :log_access
end
# in ApplicationController
def log_access
Rails.logger.info("[Access Log] Users Controller access at #{Time.now}")
end
Saturday, April 2, 2011
27. Controllers ➤ Conventions
class ProductsController < ApplicationController
def index
# GET /products
end def update
def show
# GET /products/:id
end
☹ # ... update occurred
@parent.children.each ...
end
def new
# GET /products/new
end
def edit
def create @product = Product.find(params[:id])
# POST /products end
☹
end
def show
def edit @product = Product.find(params[:id])
# GET /products/:id/edit
end
end
def destroy
def update
# PUT /products/:id @product = Product.find(params[:id])
end end
def destroy
# DELETE /products/:id
end
end
☺ before_filter :load_product
Saturday, April 2, 2011
28. Controllers ➤ Conventions
class ProductsController < ApplicationController
def index
# GET /products
end
def update
☹
def show
# GET /products/:id
# ... update occurred
# renders /app/views/products/show.format @parent.children.each ...
end end
def new
# GET /products/new
end
def edit
def create @product = Product.find(params[:id])
# POST /products end
☹
redirect_to products_url
end def show
@product = Product.find(params[:id])
def edit
end
# GET /products/:id/edit
end
def destroy
def update @product = Product.find(params[:id])
# PUT /products/:id end
end
def destroy
# DELETE /products/:id
end
☺ before_filter :load_product
end
Saturday, April 2, 2011
29. Views
Layouts & Helpers
Forms
Partials
ActionView Helpers
Saturday, April 2, 2011
30. Views ➤ Layouts & Helpers
def show
@product = Product.find(params[:id])
end M C
<!-- app/views/products/show.html.erb -->
<h2><%= @product.name %></h2>
V
<p>Price: <%= @product.price %></p>
<!-- app/layouts/application.html.erb -->
<div id="container">
<%= yield %>
</div>
<div id="container">
<h2>Pop-Tarts</h2>
<p>Price: $3.99</p>
</div>
Saturday, April 2, 2011
39. Resources for Learning
Video
PeepCode
RailsCasts
CodeSchool
Books
The Rails Way
Beginning Ruby
Ruby for _________
Other
PHX Rails User Group
Gangplank
Saturday, April 2, 2011
40. @claytonlz
http://spkr8.com/t/7007
Saturday, April 2, 2011