SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais de Richard Schneeman

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 3Richard Schneeman
 
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 2Richard Schneeman
 
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 1Richard Schneeman
 

Mais de 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
 

Último

WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxpadhand000
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theorydrae5
 
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morcall Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morvikas rana
 
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)Delhi Call girls
 
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,dollysharma2066
 
The Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushThe Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushShivain97
 
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girlsPooja Nehwal
 
LC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfLC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfpastor83
 
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)Delhi Call girls
 
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...PsychicRuben LoveSpells
 
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)Delhi Call girls
 
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)Delhi Call girls
 

Último (15)

WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptx
 
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theory
 
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morcall Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
 
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
 
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
 
The Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushThe Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by Mindbrush
 
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
 
LC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfLC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdf
 
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
 
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
 
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
 
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
 
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
 

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

Notas do Editor