metawhere at the ruby drink-up of sophia, october 2011

27
MetaWhere Stéroïdes pour ActiveRecord

Upload: rivierarb

Post on 03-Jul-2015

697 views

Category:

Technology


3 download

DESCRIPTION

ActiveRecord query syntax on steroids. Presented at the Ruby Drink-up of Sophia Antipolis on the 4th of October 2011 by Pierre Schambacher (@PierreSchambac). http://rivierarb.fr/2011/10/04/Drinkup/

TRANSCRIPT

Page 1: MetaWhere at the Ruby Drink-up of Sophia, October 2011

MetaWhereStéroïdes pour ActiveRecord

Page 2: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Exemple de modèle

User Account

PostCategory

Comment

Page 3: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Page 4: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Je veux les Posts d’aujourd’hui

Quel problème ?

Page 5: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Je veux les Posts d’aujourd’hui

Post.where(:date => Date.today)

Quel problème ?

Page 6: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Je veux les Posts d’aujourd’hui

Post.where(:date => Date.today)

Je veux les Posts des 7 derniers jours

Quel problème ?

Page 7: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Je veux les Posts d’aujourd’hui

Post.where(:date => Date.today)

Je veux les Posts des 7 derniers jours

Post.where(“date >= #{Date.today - 1.week}“)

Quel problème ?

Page 8: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Je veux les Posts d’aujourd’hui

Post.where(:date => Date.today)

Je veux les Posts des 7 derniers jours

Post.where(“date >= #{Date.today - 1.week}“)

Quel problème ?

Page 9: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Page 10: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Posts de User#42 ou de User#1337

Page 11: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Posts de User#42 ou de User#1337

Post.where(“user_id = 42 OR user_id = 1337“)

Page 12: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Posts de User#42 ou de User#1337

Post.where(“user_id = 42 OR user_id = 1337“)

Page 13: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Posts de User#42 ou de User#1337

Post.where(“user_id = 42 OR user_id = 1337“)

Posts des utilisateurs ayant un compte créé il y a plus de 3 mois

Page 14: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Posts de User#42 ou de User#1337

Post.where(“user_id = 42 OR user_id = 1337“)

Posts des utilisateurs ayant un compte créé il y a plus de 3 mois

Post.joins(:something).where(“...“)

Page 15: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Quel problème ?

Posts de User#42 ou de User#1337

Post.where(“user_id = 42 OR user_id = 1337“)

Posts des utilisateurs ayant un compte créé il y a plus de 3 mois

Post.joins(:something).where(“...“)

Page 16: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Utiliser ActiveRecord => écrire du SQL

Page 17: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Utiliser ActiveRecord => écrire du SQL

Page 19: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Méthodes sur les Symbols

Post.where(:date.gte => Date.today-1.week)

Comment.where(:rating.ne => nil)

Category.where(:name.in => [‘Ruby’, ‘Rails’])

Page 20: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Surcharge du | pour Hash

Post.where({:user_id => 42} | {:user_id => 1337})

User.where({:name.ne => ‘Bob’} | {:admin => true})

Page 21: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Simplification du joins

Page 22: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Simplification du joins

Joins avec ActiveRecord ?

Page 23: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Simplification du joins

Joins avec ActiveRecord ?

Page 24: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Simplification du joins

Joins avec ActiveRecord ?

MetaWhere:

Post.joins(:user => :accounts).where(:user => { :accounts => { :created_at.lte => Date.today - 3.months }})

Page 25: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Plus loin !

Surcharge des opérateurs

:date >= Date.today

:value ^ nil

Méthodes pour order et joins également

joins(:accounts.outer)

order(:created_at.desc)

Page 26: MetaWhere at the Ruby Drink-up of Sophia, October 2011

Amusons nous

Comment .joins( :post => [:categories, :user]) where(:post => {:categories => :name.in => [‘A’, ‘B’]}) where(:post => {:user => {:created_at.lte => Date.yesterday}}) where({:rating => 5} | {:rating => 0})

Page 27: MetaWhere at the Ruby Drink-up of Sophia, October 2011

MetaWhereMerci de votre attention.

Questions ?