SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Rails Summer of Code
                                     Week 3




Richard Schneeman - @ThinkBohemian
Rails - Week 3
              • Rails - A place for Everything
               • Stylesheets, CSS, JS, Images
               • Functional Testing - Controllers
               • Associations
                 • has_many & belongs_to
               • Controllers Vs. Models
Richard Schneeman - @ThinkBohemian
Rails - Week 3
        • Rails - A Place for Everything
         • Public
           • Images
           • Javascripts
           • Stylesheets


Richard Schneeman - @ThinkBohemian
Rails - Week 3
        • Rails - A Place for Everything
         • Public
           • Images
           • Javascripts
           • Stylesheets


Richard Schneeman - @ThinkBohemian
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’ %>



Richard Schneeman - @ThinkBohemian
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 ?




Richard Schneeman - @ThinkBohemian
Rails - Week 3
       • Use HTTP to send data
          •   get, post, put, head, delete

       • Verify Response
        • Assigns
        • Cookies
        • Flash
        • Session
Richard Schneeman - @ThinkBohemian
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

Richard Schneeman - @ThinkBohemian
Rails - Week 3
        • Use HTTP to send data
        • Get, Post, Put, Delete
        • assigns(:post) = @post
        def test_should_create_post
        
    assert_difference('Post.count') do
        
     post :create, :post => { :title => 'Some title'}
        
    end
        
    assert_redirected_to post_path(assigns(:post))
        end

Richard Schneeman - @ThinkBohemian
Associations
 • Relational Databases
  • Primary Key
    • unique key can identify each
           row in a table
    • Foreign Key
     • Relates a row to another row’s
           primary key


Richard Schneeman - @ThinkBohemian
Belongs_To
 • belongs_to :parent_class
  • Sets Foreign Key




Richard Schneeman - @ThinkBohemian
Has_Many
 • has_many :child_class
  • Builds Association in Ruby




Richard Schneeman - @ThinkBohemian
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




Richard Schneeman - @ThinkBohemian
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)
       >> myCustomer.orders




Richard Schneeman - @ThinkBohemian
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

Richard Schneeman - @ThinkBohemian
Associations
     • N+1 - (Cont.)
        customers = Customer.all
        customers.each do |customer|
          customer.orders do |order|
            order.products do |product|
              puts product
            end
          end
        end

        This would generate 10,001 database queries
                     (not a good thing)
Richard Schneeman - @ThinkBohemian
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

Richard Schneeman - @ThinkBohemian
Has_Many :Through =>
    • Chain Associations Using :Through




   Can you guess what Classes have foreign keys?
Richard Schneeman - @ThinkBohemian
Has_Many :Through =>
    • Appointments
     • physician_id
     • patient_id



      Can you guess what Classes have foreign keys?
Richard Schneeman - @ThinkBohemian
Has_Many :Through =>
    • Physician Class
     • Uses Appointments
     • Finds Patients
       • Automatically

                       >> dr = Physicians.first
                       >> dr.patients


Richard Schneeman - @ThinkBohemian
Has & Belongs To Many
    • HABTM (has and belongs to many)
     • Creates direct many to many relationship




Like using :through with a single purpose table: assemblies_parts
Richard Schneeman - @ThinkBohemian
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
Richard Schneeman - @ThinkBohemian
Questions?
                       http://guides.rubyonrails.org
                        http://stackoverflow.com



Richard Schneeman - @ThinkBohemian
Rails - Week 3
                               MVC
                 Everyone Get Your Thinking Caps on



                                           Some Rails
                                             Code




Richard Schneeman - @ThinkBohemian

Weitere ähnliche Inhalte

Mehr von Richard Schneeman

Mehr von Richard Schneeman (6)

Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3
 
Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2
 
Rails 3 Beginner to Builder 2011 Week 1
Rails 3 Beginner to Builder 2011 Week 1Rails 3 Beginner to Builder 2011 Week 1
Rails 3 Beginner to Builder 2011 Week 1
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4 UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4
 
UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1
 

UT on Rails3 2010- Week 3

  • 1. Rails Summer of Code Week 3 Richard Schneeman - @ThinkBohemian
  • 2. Rails - Week 3 • Rails - A place for Everything • Stylesheets, CSS, JS, Images • Functional Testing - Controllers • Associations • has_many & belongs_to • Controllers Vs. Models Richard Schneeman - @ThinkBohemian
  • 3. Rails - Week 3 • Rails - A Place for Everything • Public • Images • Javascripts • Stylesheets Richard Schneeman - @ThinkBohemian
  • 4. Rails - Week 3 • Rails - A Place for Everything • Public • Images • Javascripts • Stylesheets Richard Schneeman - @ThinkBohemian
  • 5. 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’ %> Richard Schneeman - @ThinkBohemian
  • 6. 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 ? Richard Schneeman - @ThinkBohemian
  • 7. Rails - Week 3 • Use HTTP to send data • get, post, put, head, delete • Verify Response • Assigns • Cookies • Flash • Session Richard Schneeman - @ThinkBohemian
  • 8. 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 Richard Schneeman - @ThinkBohemian
  • 9. Rails - Week 3 • Use HTTP to send data • Get, Post, Put, Delete • assigns(:post) = @post def test_should_create_post assert_difference('Post.count') do post :create, :post => { :title => 'Some title'} end assert_redirected_to post_path(assigns(:post)) end Richard Schneeman - @ThinkBohemian
  • 10. Associations • Relational Databases • Primary Key • unique key can identify each row in a table • Foreign Key • Relates a row to another row’s primary key Richard Schneeman - @ThinkBohemian
  • 11. Belongs_To • belongs_to :parent_class • Sets Foreign Key Richard Schneeman - @ThinkBohemian
  • 12. Has_Many • has_many :child_class • Builds Association in Ruby Richard Schneeman - @ThinkBohemian
  • 13. 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 Richard Schneeman - @ThinkBohemian
  • 14. 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) >> myCustomer.orders Richard Schneeman - @ThinkBohemian
  • 15. 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 Richard Schneeman - @ThinkBohemian
  • 16. Associations • N+1 - (Cont.) customers = Customer.all customers.each do |customer| customer.orders do |order| order.products do |product| puts product end end end This would generate 10,001 database queries (not a good thing) Richard Schneeman - @ThinkBohemian
  • 17. 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 Richard Schneeman - @ThinkBohemian
  • 18. Has_Many :Through => • Chain Associations Using :Through Can you guess what Classes have foreign keys? Richard Schneeman - @ThinkBohemian
  • 19. Has_Many :Through => • Appointments • physician_id • patient_id Can you guess what Classes have foreign keys? Richard Schneeman - @ThinkBohemian
  • 20. Has_Many :Through => • Physician Class • Uses Appointments • Finds Patients • Automatically >> dr = Physicians.first >> dr.patients Richard Schneeman - @ThinkBohemian
  • 21. Has & Belongs To Many • HABTM (has and belongs to many) • Creates direct many to many relationship Like using :through with a single purpose table: assemblies_parts Richard Schneeman - @ThinkBohemian
  • 22. 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 Richard Schneeman - @ThinkBohemian
  • 23. Questions? http://guides.rubyonrails.org http://stackoverflow.com Richard Schneeman - @ThinkBohemian
  • 24. Rails - Week 3 MVC Everyone Get Your Thinking Caps on Some Rails Code Richard Schneeman - @ThinkBohemian

Hinweis der Redaktion