MetaWhere at the Ruby Drink-up of Sophia, October 2011
-
Upload
rivierarb -
Category
Technology
-
view
697 -
download
3
description
Transcript of MetaWhere at the Ruby Drink-up of Sophia, October 2011
![Page 1: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/1.jpg)
MetaWhereStéroïdes pour ActiveRecord
![Page 2: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/2.jpg)
Exemple de modèle
User Account
PostCategory
Comment
![Page 3: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/3.jpg)
Quel problème ?
![Page 4: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/4.jpg)
Je veux les Posts d’aujourd’hui
Quel problème ?
![Page 5: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/5.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/6.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/7.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/8.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/9.jpg)
Quel problème ?
![Page 10: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/10.jpg)
Quel problème ?
Posts de User#42 ou de User#1337
![Page 11: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/11.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/12.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/13.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/14.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/15.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/16.jpg)
Utiliser ActiveRecord => écrire du SQL
![Page 17: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/17.jpg)
Utiliser ActiveRecord => écrire du SQL
![Page 18: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/18.jpg)
MetaWhere
http://erniemiller.org/projects/metawhere/
https://github.com/ernie/meta_where
https://github.com/ernie/squeel
![Page 19: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/19.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/20.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/21.jpg)
Simplification du joins
![Page 22: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/22.jpg)
Simplification du joins
Joins avec ActiveRecord ?
![Page 23: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/23.jpg)
Simplification du joins
Joins avec ActiveRecord ?
![Page 24: MetaWhere at the Ruby Drink-up of Sophia, October 2011](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/24.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/25.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/26.jpg)
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](https://reader038.fdocuments.net/reader038/viewer/2022110122/55959fc21a28ab00448b45bc/html5/thumbnails/27.jpg)
MetaWhereMerci de votre attention.
Questions ?