ActiveRecord Query Interface

  • 3,693 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,693
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
31
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ActiveRecord Query Interface 3.0
  • 2. @tim_keller
  • 3. Consultant at innoQ
  • 4. OK. Let's Go!
  • 5. In a Nutshell
  • 6. The new AR Query Interface is:
  • 7. Tasty!
  • 8. That was it.
  • 9. Just kidding!
  • 10. Preamble
  • 11. The new Query Interface is build on top of:
  • 12. Active Relation
  • 13. A GoogleSummerOfCode-Project that:
  • 14. „...integrate Arel into the existing implementation of ActiveRecord...“ http://socghop.appspot.com/gsoc/student_project/show/google/gsoc2009/rails/t124025364581
  • 15. „Arel is a Relational Algebra for Ruby“ http://github.com/rails/arel
  • 16. And simplifies the generation of complex SQL queries
  • 17. Show me some code!
  • 18. Caution!
  • 19. The „old“ ActiveRecord Query Interface will be deprecated in Rails 3.1 and fully removed in Rails 3.2!
  • 20. The „options hash“ for ActiveRecord class methods is now deprecated
  • 21. MyClass.find(:first) and MyClass.find(:last) are also deprecated
  • 22. Example (deprecated) ● Owner.find(:first) ● Owner.find(:first, :conditions => {:name => 'test'}) But that is OK: ● Owner.find(1) ● Owner.find(1,2)
  • 23. The „options hash“ for the Scope stuff is also deprecated
  • 24. Example (deprecated) ● named_scope :red, :conditions => { :colour => 'red' } ● default_scope :order => 'last_name'
  • 25. „named_scope“ is deprecated in Rails 3.0. Just use „scope“
  • 26. The New Shit!
  • 27. New finder methods
  • 28. Example (New API) ● where ● having ● select ● group ● order ● limit ● offset ● joins ● includes ● lock ● readonly ● from
  • 29. The return value is a ActiveRecord::Relation
  • 30. Relations are lazy loaded!
  • 31. Example ● Rails 2 ● Owner.find(:first, :conditions => {:name => 'test'}) ● Rails 3 ● Owner.where(:name => 'test') ● with DB-Query – Owner.where(:name => 'test').each{ |o| puts o.name }
  • 32. For Eager Loading use all(), first(), last()
  • 33. Example ● Rails 2 ● Owner.find(:first, :conditions => {:name => 'test'}) ● Rails 3 ● Owner.where(:name => 'test').first
  • 34. Relations are chainable and „shareable“
  • 35. Example ● Rails 3 @books = Book.order(params[:order)) @published_books = @books.where(:published => true) @unpublished_books = @books.where(:published => false)
  • 36. Example ● Rails 2 ● Book.find(:all, :conditions => ["price <= ?", 10], :include => :authors) ● Rails 3 ● Book.where("price <= ?",10).includes(:authors)
  • 37. named_scope => scope
  • 38. Example ● Rails 2 ● named_scope :red, :conditions => { :colour => 'red' } ● Rails 3.0 ● scope :red, :conditions => { :colour => 'red' } ● Rails 3.2 ● scope :red, where(:colour => 'red')
  • 39. Sources ● m.onkey.org/2010/1/22/active-record-query- interface ● magicscalingsprinkles.wordpress.com/2010/01/ 28/why-i-wrote-arel/ ● edgeapi.rubyonrails.org/ ● slideshare.net/GreggPollack/rails-3-beautiful- code-3219240 ● flickr.com/photos/fofurasfelinas/2502823019/siz es/o/
  • 40. Sources ● flickr.com/photos/timcaynes/148110505/ ● flickr.com/photos/gurana/3831997459/sizes/m/
  • 41. My two cents. Thank you!