Beginner to Builder                        Week 3                        Richard Schneeman                        @schneem...
Austin on Rails                        • Tuesday, 27th 7:00pm                          • 7th and Brazos 8th Floor         ...
Rails - Week 3                        • Ruby                          • Variables                        • Rails          ...
Variables                        foobar     # local variable                        @foobar    # instance variable, starts...
Instance Variables         @color = "blue"           color = "pink"         def instance_fur_color    def local_fur_color ...
Instance Variables         @color = "blue"           color = "pink"         def instance_fur_color    def local_fur_color ...
Instance Variables                        class Dog                          def set_color(color)                         ...
Bad Scope         class Circle           $sides = 0           def self.sides             $sides           end             ...
Rails - Week 3                • Rails - A Place for Everything                  • Public                    • Images      ...
Rails - Week 3                • Rails - A Place for Everything                • View Helpers                        • <%= ...
Controllers                        • The C in MVC                          • handles buisness logic                 htt://...
Controllers                        • Pass data to controllers via query                          string               htt:...
Controllers                        • params, get data from the url                        • instance variables pass data t...
Controllers                        • Receive Arrays               Arrays                   GET /clients?ids[]=1&ids[]=2&id...
Controllers    Form                        • Receive Hashes from forms      <form action="/clients" method="post">        ...
Controllers                        • Control application flow                        • render content                      ...
Rails - Week 3                • Functional Testing - Controllers                        •   web request successful?       ...
Rails - Week 3               • Use HTTP to send data to controller                        •   get, post, put, head, delete...
Cookies?                        • Stored on the user’s computer                        • Mostly used for settings         ...
Session?                        • store and access user specific data                        • Uses cookies to store data b...
Flash?                        • Part of the session                        • cleared after each request                   ...
Flash?              flash[:notice] = "You must sign in to see this page."                                        or       ...
Rails - Week 3                  • Functional Testing - Controllers                    • Send data to controller           ...
Rails - Week 3                 • Use HTTP to send data                 • Get, Post, Put, Delete       Controller          ...
Rails - Week 3                 • Assert difference                assert_difference(Post.count, 1) do                  post...
Rails - Week 3@SchneemsFriday, June 24, 2011
Associations   • Relational Databases     • Primary Key       • unique key can identify         each row in a table     • ...
Primary Key                        • Unique identifier for all objects                            Hats:                    ...
Foreign Key                        • Relates to another row’s primary key                            Hats                 ...
Foreign Key                        • Creates relationships                                    Inspectors                  ...
Belongs_To   • belongs_to :parent_class     • Sets Foreign Key@SchneemsFriday, June 24, 2011
Has_Many   • has_many :child_class     • Builds Association in Ruby                        i.e. Hat inspector has many hat...
Has_Many   • How Does this Help?     • Related objects contain links to one       another     • Get one object, you’ve got...
Has_Many         • Caveats         •       myCustomer.orders builds SQL and hits the database               • N+1 Problem ...
Associations         • N+1 - (Cont.)                        customers = Customer.all                        customers.each...
Associations         • N+1 - How do we Fix it?           • What if we could Pre-load Associations?             • Includes ...
Has_Many :Through =>         • Chain Associations Using :Through                           What Classes have foreign keys?...
Has_Many :Through =>        • Appointments          • physician_id          • patient_id                            What C...
Has_Many :Through =>        • Physician Class          • Uses Appointments          • Finds Patients            • Automati...
Has & Belongs To Many        • HABTM (has and belongs to many)          • Creates direct many to many relationship        ...
HABTM - Vs. :Through        • Use HABTM        • Don’t need to do anything with the relationship          model          •...
Questions?                   http://guides.rubyonrails.org                   http://stackoverflow.com@SchneemsFriday, June ...
Upcoming SlideShare
Loading in...5
×

Rails 3 Beginner to Builder 2011 Week 3

11,778

Published on

This is the 3rd of 8 presentations given at University of Texas during my Beginner to Builder Rails 3 Class. For more info and the whole series including video presentations at my blog:


http://schneems.tumblr.com/tagged/Rails-3-beginner-to-builder-2011

Published in: Education, Technology, Design
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total Views
11,778
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
134
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Rails 3 Beginner to Builder 2011 Week 3

  1. 1. Beginner to Builder Week 3 Richard Schneeman @schneemsJune, 2011Friday, June 24, 2011
  2. 2. Austin on Rails • Tuesday, 27th 7:00pm • 7th and Brazos 8th Floor • @austinonrails@SchneemsFriday, June 24, 2011
  3. 3. Rails - Week 3 • Ruby • Variables • Rails • JS/CSS/images • Controllers • Controller Testing • Database Associations@SchneemsFriday, June 24, 2011
  4. 4. Variables foobar # local variable @foobar # instance variable, starts with @ @@foobar # class variable, starts with @@ $foobar # global variable, starts with $ FOOBAR # Constant, starts with a capitol letter FooBar # Classes are constants@SchneemsFriday, June 24, 2011
  5. 5. Instance Variables @color = "blue" color = "pink" def instance_fur_color def local_fur_color return @color return color end end puts instance_fur_color puts local_fur_color >> "blue" >> NameError: undefined local variable@SchneemsFriday, June 24, 2011
  6. 6. Instance Variables @color = "blue" color = "pink" def instance_fur_color def local_fur_color return @color return defined?(color) end end puts instance_fur_color >> False >> "blue"@SchneemsFriday, June 24, 2011
  7. 7. Instance Variables class Dog def set_color(color) @fur_color = color end def color "fur color is #{@fur_color}" end end # create a Dog instance lassie = Dog.new # set color lassie.set_color("brown") # get @fur_color lassie.color >> "@fur_color is brown"@SchneemsFriday, June 24, 2011
  8. 8. Bad Scope class Circle $sides = 0 def self.sides $sides end Triangle.sides end >> 3 Circle.sides class Triangle >> 3 $sides = 3 def self.sides $sides end end@SchneemsFriday, June 24, 2011
  9. 9. Rails - Week 3 • Rails - A Place for Everything • Public • Images • Javascripts • Stylesheets@SchneemsFriday, June 24, 2011
  10. 10. Rails - Week 3 • Rails - A Place for Everything • View Helpers • <%= stylesheet_link_tag :all %> • <%= javascript_include_tag :defaults %> • Require Specific Files • <%= image_tag ‘rails.png’ %> • <%= stylesheet_link_tag ‘scaffold.css’ %> • <%= javascript_include_tag ‘rails.js’ %>@SchneemsFriday, June 24, 2011
  11. 11. Controllers • The C in MVC • handles buisness logic htt://localhost:3000/clients/new class ClientsController < ApplicationController def new end end@SchneemsFriday, June 24, 2011
  12. 12. Controllers • Pass data to controllers via query string htt://localhost:3000/clients?status=activated ?status=activated ?status=activated&level=23 ?status=deactivated&level=1&foo=bar@SchneemsFriday, June 24, 2011
  13. 13. Controllers • params, get data from the url • instance variables pass data to view http://localhost:3000/clients?status=activated def index @status = params[:status] if @status == "activated" @clients = Client.activated else @clients = Client.unactivated end end@SchneemsFriday, June 24, 2011
  14. 14. Controllers • Receive Arrays Arrays GET /clients?ids[]=1&ids[]=2&ids[]=3 params[:ids] # => [“1”,”2”,”3”]@SchneemsFriday, June 24, 2011
  15. 15. Controllers Form • Receive Hashes from forms <form action="/clients" method="post"> <input type="text" name="client[name]" value="Acme" /> <input type="text" name="client[phone]" value="12345" /> <input type="text" name="client[address][postcode]" value="12345" /> </form> Hash params[:client] # => {:name => “Acme”, :phone => “12345”, :address => {:postcode => “12345”} }@SchneemsFriday, June 24, 2011
  16. 16. Controllers • Control application flow • render content htt://localhost:3000/clients/new # send to another action/url redirect_to "/home" # render content render :action => "new" render :layout => "awesome" render :text => "foo" render :json => {}@SchneemsFriday, June 24, 2011
  17. 17. Rails - Week 3 • Functional Testing - Controllers • web request successful? • user redirected to the right page? • user successfully authenticated? • correct object stored in the template? • appropriate message displayed to the user ?@SchneemsFriday, June 24, 2011
  18. 18. Rails - Week 3 • Use HTTP to send data to controller • get, post, put, head, delete • Verify Response • Assigns • Cookies • Flash • Session@SchneemsFriday, June 24, 2011
  19. 19. Cookies? • Stored on the user’s computer • Mostly used for settings cookies["remember_me"] = true@SchneemsFriday, June 24, 2011
  20. 20. Session? • store and access user specific data • Uses cookies to store data by default • very quick, and secure • NOT for critical information • Put it in the database session[:logged_in] = true session[:user_id] = 57 session[:is_mobile] = false@SchneemsFriday, June 24, 2011
  21. 21. Flash? • Part of the session • cleared after each request • useful for error messages@SchneemsFriday, June 24, 2011
  22. 22. Flash? flash[:notice] = "You must sign in to see this page." or redirect_to signup_url, :notice => "You must sign in to see this page."@SchneemsFriday, June 24, 2011
  23. 23. Rails - Week 3 • Functional Testing - Controllers • Send data to controller • Verify response class PetControllerTest < ActionController::TestCase test "should get index" do get :index assert_response :success end end@SchneemsFriday, June 24, 2011
  24. 24. Rails - Week 3 • Use HTTP to send data • Get, Post, Put, Delete Controller • assigns(:post) = @post def create @post = Post.create(params[:post]) end Test post :create, :post => { :title => Some title} assert assigns(:post).is_a?(Post)@SchneemsFriday, June 24, 2011
  25. 25. Rails - Week 3 • Assert difference assert_difference(Post.count, 1) do post :create, :post => { :title => Some title} end@SchneemsFriday, June 24, 2011
  26. 26. Rails - Week 3@SchneemsFriday, June 24, 2011
  27. 27. Associations • Relational Databases • Primary Key • unique key can identify each row in a table • Foreign Key • Relates a row keyanother row’s primary to@SchneemsFriday, June 24, 2011
  28. 28. Primary Key • Unique identifier for all objects Hats: id: 557 style: "Fedora" Hats: id: 687 style: "Cowboy"@SchneemsFriday, June 24, 2011
  29. 29. Foreign Key • Relates to another row’s primary key Hats id: 557 style: "Fedora" inspector_id: 35 Inspectors id: 35 name: "John"@SchneemsFriday, June 24, 2011
  30. 30. Foreign Key • Creates relationships Inspectors id: 35 name: "John" Hats Hats Hats id: 557 id: 558 id: 559 style: "Fedora" style: "Ballcap" style: "Silly" inspector_id: 35 inspector_id: 35 inspector_id: 35@SchneemsFriday, June 24, 2011
  31. 31. Belongs_To • belongs_to :parent_class • Sets Foreign Key@SchneemsFriday, June 24, 2011
  32. 32. Has_Many • has_many :child_class • Builds Association in Ruby i.e. Hat inspector has many hats@SchneemsFriday, June 24, 2011
  33. 33. Has_Many • How Does this Help? • Related objects contain links to one another • Get one object, you’ve got all associated >> myCustomer = Customer.where(:id => 2) >> orders = myCustomer.orders.all@SchneemsFriday, June 24, 2011
  34. 34. Has_Many • Caveats • myCustomer.orders builds SQL and hits the database • N+1 Problem - Imagine • You query 100 customers • Each Customer has 100 orders • Each Order has 100 products • ...@SchneemsFriday, June 24, 2011
  35. 35. Associations • N+1 - (Cont.) customers = Customer.all customers.each do |customer| customer.orders do |order| order.products do |product| puts product end end This would generate 10,001 database queries (not a good thing)@SchneemsFriday, June 24, 2011
  36. 36. Associations • N+1 - How do we Fix it? • What if we could Pre-load Associations? • Includes Customer.includes(:orders => :products).all This would generate 1 database query!! Will take significantly less time than alternative Note: database access is almost always be your bottleneck@SchneemsFriday, June 24, 2011
  37. 37. Has_Many :Through => • Chain Associations Using :Through What Classes have foreign keys?@SchneemsFriday, June 24, 2011
  38. 38. Has_Many :Through => • Appointments • physician_id • patient_id What Classes have foreign keys?@SchneemsFriday, June 24, 2011
  39. 39. Has_Many :Through => • Physician Class • Uses Appointments • Finds Patients • Automatically >> dr = Physicians.first >> dr.patients@SchneemsFriday, June 24, 2011
  40. 40. Has & Belongs To Many • HABTM (has and belongs to many) • Creates direct many to many relationship This is essentially using :through with a@Schneems single purpose table: assemblies_partsFriday, June 24, 2011
  41. 41. HABTM - Vs. :Through • Use HABTM • Don’t need to do anything with the relationship model • Restricted DB size • Use :through • Need validations • Need callbacks • Need extra attributes@SchneemsFriday, June 24, 2011
  42. 42. Questions? http://guides.rubyonrails.org http://stackoverflow.com@SchneemsFriday, June 24, 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×