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/
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
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