ActiveRecord Query Interface 3.0
@tim_keller
Consultant at innoQ
OK. Let's Go!
In a Nutshell
The new AR Query Interface is:
Tasty!
That was it.
Just kidding!
Preamble
The new Query Interface is build on
            top of:
Active Relation
A GoogleSummerOfCode-Project
           that:
„...integrate Arel into the existing
    implementation of ActiveRecord...“




http://socghop.appspot.com/gsoc/student_pr...
„Arel is a Relational Algebra for
                       Ruby“




http://github.com/rails/arel
And simplifies the generation of
    complex SQL queries
Show me some code!
Caution!
The „old“ ActiveRecord Query
Interface will be deprecated in Rails
 3.1 and fully removed in Rails 3.2!
The „options hash“ for ActiveRecord
 class methods is now deprecated
MyClass.find(:first) and
MyClass.find(:last) are also
        deprecated
Example (deprecated)
●   Owner.find(:first)
●   Owner.find(:first, :conditions =>
    {:name => 'test'})


But that is OK:...
The „options hash“ for the Scope
    stuff is also deprecated
Example (deprecated)
●   named_scope :red, :conditions =>
    { :colour => 'red' }
●   default_scope :order => 'last_name'
„named_scope“ is deprecated in
   Rails 3.0. Just use „scope“
The New Shit!
New finder methods
Example (New API)
●   where
●   having
●   select
●   group
●   order
●   limit
●   offset
●   joins
●   includes
●   lock...
The return value is a
ActiveRecord::Relation
Relations are lazy loaded!
Example
●   Rails 2
    ●   Owner.find(:first, :conditions =>
        {:name => 'test'})


●   Rails 3
    ●   Owner.where...
For Eager Loading use all(),
     first(), last()
Example
●   Rails 2
    ●   Owner.find(:first, :conditions =>
        {:name => 'test'})


●   Rails 3
    ●   Owner.where...
Relations are chainable and
        „shareable“
Example
●   Rails 3
    @books = Book.order(params[:order))
    @published_books = @books.where(:published =>
    true)
  ...
Example
●   Rails 2
    ●   Book.find(:all, :conditions => ["price
        <= ?", 10], :include => :authors)


●   Rails 3...
named_scope => scope
Example
●   Rails 2
    ●   named_scope :red, :conditions => {
        :colour => 'red' }
●   Rails 3.0
    ●   scope :red...
Sources
●   m.onkey.org/2010/1/22/active-record-query-
    interface
●   magicscalingsprinkles.wordpress.com/2010/01/
    ...
Sources
●   flickr.com/photos/timcaynes/148110505/
●   flickr.com/photos/gurana/3831997459/sizes/m/
My two cents. Thank you!
Upcoming SlideShare
Loading in …5
×

ActiveRecord Query Interface

4,123 views
3,949 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
4,123
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ActiveRecord Query Interface

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

×