Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Arel

Introduction to Arel

Arel fundamentals

Bruno Sutic

October 28, 2014
Tweet

More Decks by Bruno Sutic

Other Decks in Programming

Transcript

  1. Introduction to Arel

    View Slide

  2. Bruno Sutic

    Rails & Javascript developer


    Ideal Project Group, Chicago
    github.com/bruno-

    @brunosutic

    View Slide

  3. What is Arel?
    • github.com/rails/arel
    • “Simplifies generation of complex SQL”

    You write Ruby, out comes the SQL
    • dependency of ActiveRecord
    • can be used together with ActiveRecord

    View Slide

  4. How I learned about Arel?
    • credit: Janko Marohnic
    • used Arel to optimize a feature on a project

    View Slide

  5. Demo to get you interested :)

    View Slide

  6. Prerequisites for the
    examples
    • ‘Customer’ is an ActiveRecord model
    • Arel api for a column:

    Customer.arel_table[:name]
    • A “convention”:

    class Customer

    def self.[](column)

    arel_table[column]

    end

    end
    Customer[:name]

    View Slide

  7. Basic example
    • ActiveRecord

    Customer.where(name: variable)
    • Arel

    Customer.where(Customer[:name].eq(variable))

    View Slide

  8. A bit more useful example
    • ActiveRecord

    Customer.joins(:sales)

    .where(“sales.price” => variable)


    or


    Customer.joins(:sales)

    .where(sales: { price: variable })
    • Arel

    Customer.joins(:sales)

    .where(Sale[:price].eq(variable))

    View Slide

  9. SQL comparisons example
    • ActiveRecord

    Customer.where(“credits > ?”, credits_variable)
    • Arel

    Customer.where(

    Customer[:credits].gt(credits_variable)

    )
    • All comparison operators:

    gt, lt, gteq, lteq

    View Slide

  10. OR condition example
    • ActiveRecord

    Customer.where(“credits > ? OR credits < ?”, var1, var2)
    • Arel

    Customer.where(

    Customer[:credits].gt(var1).or(

    Customer[:credits].lt(var2)

    )

    )

    View Slide

  11. Complex conditions example
    • ActiveRecord

    Customer.where(

    “credits > ? OR (name = ? AND credits < ?)”, var1, var2, var3

    )
    • Arel

    Customer.where(

    Customer[:credits].gt(var1).or(

    Customer[:name].eq(var2).and(

    Customer[:credits].lt(var3)

    )

    )

    )

    View Slide

  12. LIKE example
    • ActiveRecord

    Customer.where(“name LIKE ?”, “%#{variable}%”)
    • Arel

    Customer.where(Customer[:name].matches(“%#{variable}%”))

    View Slide

  13. Other features
    • support for all SQL ‘join’ types
    • SQL literals
    • writing only a part of a bigger query as SQL
    • example: CASE statement

    View Slide

  14. Benefits of using Arel
    • pure Ruby, nicer looking code
    • proper code highlighting
    • ruby exceptions
    • completely avoids SQL injection

    View Slide

  15. Arel resources
    • github repo:

    https://github.com/rails/arel
    • great speech:

    https://www.youtube.com/watch?v=ShPAxNcLm3o
    • source code walkthrough:

    https://www.youtube.com/watch?v=EJ6b_2S9Ids
    • examples from this talk:

    https://gist.github.com/bruno-/5964403476c791331c49

    View Slide

  16. Questions?

    View Slide