• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Arel in Rails 3
 

Arel in Rails 3

on

  • 11,763 views

 

Statistics

Views

Total Views
11,763
Views on SlideShare
11,364
Embed Views
399

Actions

Likes
4
Downloads
54
Comments
0

4 Embeds 399

http://www.leonardoborges.com 377
http://localhost 13
https://twitter.com 7
http://leonardoborges.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Arel in Rails 3 Arel in Rails 3 Presentation Transcript

  • Arel and Rails 3Relational algebra meets ActiveRecord
  • What!?• Relational Algebra deals with sets of finite relations, which are closed under certain operators. These operators operate on one or more relations to yield a relation.• Relation (table) - Data structure composed of a heading (columns) and an unordered set of tuples (rows) that share the same type.• Closure - A set is said to be closed under some operation if the result of that operation is a member of the set. • e.g.: Real numbers are closed under subtraction but natural numbers are not. Although 3 and 7 are natural numbers the result of 3 - 7 is not.
  • Operators Primitive Derived Selection (WHERE) Set Intersection Projection (SELECT) Division Cross Join Natural Join Set Union Set DifferenceRename (SELECT A as B)
  • ActiveRecord::Relation Example Pub.select(:name).where(:city => Sydney) #yields an ActiveRecord::Relation instance. No SQL ran. but prior to Rails 3...Pub.find(:all, :select => "name", :conditions => { :city => Sydney }) #yields an Array with the results
  • Ok, but now what?Build more interesting queries by combining relations...> syd_pubs = Pub.select(pubs.name).where(:city => Sydney)> squire = Pub.joins(:beers).where(:beers => {:name => James Squire})Now get me all the pubs in sydney that sell James Squire!> (syd_pubs & squire).all# yields an Array
  • Arel in Action> (syd_pubs & squire).allThis causes ActiveRecord::Relation to ask Arel to build the SQLquery below, corresponding to the combined relations: SELECT pubs.name FROM pubs INNER JOIN beers_pubs ON beers_pubs.pub_id = pubs.id INNER JOIN beers ON beers.id = beers_pubs.beer_id WHERE (pubs.city = Sydney) AND (beers.name = James Squire)
  • Grouping and sorting data > pubs_beers = Pub.joins(:beers) > pubs = pubs_beers.select(pubs.name, count(*) asbeers_count).group(pubs.name) > pubs.all[0].beers_count # 2 > pubs.order(beers_count).all
  • Range Conditions> today = Time.now.at_beginning_of_day> tomorrow = Time.now.tomorrow.at_beginning_of_day> Pub.where(:created_at => today..tomorrow).allArel takes care of ranges as well... SELECT pubs.* FROM pubs WHERE (pubs.created_at BETWEEN 2010-10-05 00:00:00.000000 AND 2010-10-06 00:00:00.000000)
  • Scopes: Reusable Relationsclass Beer < ActiveRecord::Base scope :stout, where(:flavor => "Stout")end> Beer.stout.all# gives back an Array with the results
  • Eager Loading AssociationsAssociations are lazily loaded by default and as such...> pubs = Pub.all> pubs[0].beers> pubs[1].beers...produces these queries to be executed: SELECT pubs.* FROM pubs SELECT * FROM beers INNER JOIN beers_pubs ON beers.id = beers_pubs.beer_id WHERE (beers_pubs.pub_id = 1 ) SELECT * FROM beers INNER JOIN beers_pubs ON beers.id = beers_pubs.beer_id WHERE (beers_pubs.pub_id = 2 )
  • Eager Loading AssociationsEager load what you need using the new relations syntax:> pubs = Pub.includes(:beers).all> pubs[0].beers> pubs[1].beersNote that only one query is used to load the Beers info: SELECT pubs.* FROM pubs SELECT beers.*, t0.pub_id as the_parent_record_id FROM beers INNER JOIN beers_pubs t0 ON beers.id = t0.beer_id WHERE (t0.pub_id IN (1,2,3))
  • Thank you!
  • Q&A
  • References• Arel - http://github.com/rails/arel• Rails - http://github.com/rails/rails• Rails Guides - http://guides.rubyonrails.org/• Relational Algebra - http://en.wikipedia.org/wiki/Relational_algebra