Rails Sumer of Code
                                     Week 3




Richard Schneeman - @ThinkBohemian
Rails - Week 3
              • Rails - A place for Everything
               • Stylesheets, CSS, JS, Images
              ...
Rails - Week 3
        • Rails - A Place for Everything
         • Public
           • Images
           • Javascripts
   ...
Rails - Week 3
        • Rails - A Place for Everything
         • Public
           • Images
           • Javascripts
   ...
Rails - Week 3
        • Rails - A Place for Everything
        • View Helpers
           •   <%= stylesheet_link_tag :all...
Rails - Week 3
        • Functional Testing - Controllers
           •   web request successful?

           •   user redi...
Rails - Week 3
       • Use HTTP to send data
          •   get, post, put, head, delete

       • Verify Response
       ...
Rails - Week 3
        • Functional Testing - Controllers
         • Send data to controller
         • Verify response
  ...
Rails - Week 3
        • Use HTTP to send data
        • Get, Post, Put, Delete
        • assigns(:post) = @post
        d...
Associations
 • Relational Databases
  • Primary Key
    • unique key can identify each
           row in a table
    • Fo...
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 associ...
Has_Many
 • How Does this Help?
  • Related objects contain links to one another
  • Get one object, you’ve got all associ...
Has_Many
    • Caveats
    •   myCustomer.orders builds SQL and hits the database

        • N+1 Problem - Imagine
       ...
Associations
     • N+1 - (Cont.)
        customers = Customer.all
        customers.each do |customer|
          customer...
Associations
    • N+1 - How do we Fix it?
     • What if we could Pre-load Associations?
       • Includes
         Custo...
Has_Many :Through =>
    • Chain Associations Using :Through




   Can you guess what Classes have foreign keys?
Richard ...
Has_Many :Through =>
    • Appointments
     • physician_id
     • patient_id



      Can you guess what Classes have for...
Has_Many :Through =>
    • Physician Class
     • Uses Appointments
     • Finds Patients
       • Automatically

        ...
Has & Belongs To Many
    • HABTM (has and belongs to many)
     • Creates direct many to many relationship




Like using...
HABTM - Vs. :Through
    • Use HABTM
    • Don’t need to do anything with the relationship
        model
       • Restrict...
Questions?
                       http://guides.rubyonrails.org
                        http://stackoverflow.com



Richard...
Rails - Week 3
                               MVC
                 Everyone Get Your Thinking Caps on



                 ...
Upcoming SlideShare
Loading in …5
×

UT on Rails3 2010- Week 3

1,292 views

Published on

This series introduces students to ruby on rails 3 through the book "Agile Web Development with Rails" with accompanying lecture videos found at http://www.thinkbohemian.com/tag/rails-summer-of-code/

Published in: Self Improvement
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,292
On SlideShare
0
From Embeds
0
Number of Embeds
244
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
























  • UT on Rails3 2010- Week 3

    1. 1. Rails Sumer of Code Week 3 Richard Schneeman - @ThinkBohemian
    2. 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. 3. Rails - Week 3 • Rails - A Place for Everything • Public • Images • Javascripts • Stylesheets Richard Schneeman - @ThinkBohemian
    4. 4. Rails - Week 3 • Rails - A Place for Everything • Public • Images • Javascripts • Stylesheets Richard Schneeman - @ThinkBohemian
    5. 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. 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. 7. Rails - Week 3 • Use HTTP to send data • get, post, put, head, delete • Verify Response • Assigns • Cookies • Flash • Session Richard Schneeman - @ThinkBohemian
    8. 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. 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. 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. 11. Belongs_To • belongs_to :parent_class • Sets Foreign Key Richard Schneeman - @ThinkBohemian
    12. 12. Has_Many • has_many :child_class • Builds Association in Ruby Richard Schneeman - @ThinkBohemian
    13. 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. 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. 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. 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. 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. 18. Has_Many :Through => • Chain Associations Using :Through Can you guess what Classes have foreign keys? Richard Schneeman - @ThinkBohemian
    19. 19. Has_Many :Through => • Appointments • physician_id • patient_id Can you guess what Classes have foreign keys? Richard Schneeman - @ThinkBohemian
    20. 20. Has_Many :Through => • Physician Class • Uses Appointments • Finds Patients • Automatically >> dr = Physicians.first >> dr.patients Richard Schneeman - @ThinkBohemian
    21. 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. 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. 23. Questions? http://guides.rubyonrails.org http://stackoverflow.com Richard Schneeman - @ThinkBohemian
    24. 24. Rails - Week 3 MVC Everyone Get Your Thinking Caps on Some Rails Code Richard Schneeman - @ThinkBohemian

    ×