-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
68 lines (52 loc) · 2.53 KB
/
README
File metadata and controls
68 lines (52 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
SqlFilterChain
==============
Sometimes, the query planner is really dumb, and you have to
restructure your queries to run more efficiently. This plugin is to
help rewrite a certain class of these queries that match the form:
Find all elements in Table1 that match
criteria A from TableA and
criteria B from TableB and
criteria C from TableC [ and ... ]
to be more performant by transforming them into the form:
Find all elements in Table1 that match criteria A from TableA.
Of those elements, throw out any that do not match criteria B
from TableB. Of those elements, throw out any that do not match
criteria C from TableC. [ ... ]
The traditional approach for the original form is what happens when
you chain together named scopes, which are very pretty but
unfortunately often not performant.
This plugin adds a method, ActiveRecord::Base#sql_filter_chain which
returns a scope that fetches the IDs matching the desired filters,
allows you to treat it as a special named scope.
Example
=======
# These examples are taken directly from the supplied test cases.
# Given a model with two attributes, name and email:
class MyModel < ActiveRecord::Base
named_scope :bobs, :conditions => "name = 'Bob'"
named_scope :edu_emails, :conditions => "email ~* '.edu'"
named_scope :berkeley, :conditions => "email ~* 'berkeley'"
end
# Use the sql_filter_chain method to retrieve rows matching desired
# criteria efficiently by passing in named scopes to be applied in
# sequence.
>>> winner = MyModel.create :name => "Bob", :email => "bob@berkeley.edu"
>>> MyModel.create :name => "Dave", :email => "david@berkeley.edu"
>>> MyModel.create :name => "Bob", :email => "bob@identified.com"
>>> MyModel.sql_filter_chain(MyModel.bobs, MyModel.edu_emails, MyModel.berkeley) == [winner]
=> true
# Note that this still works with named scopes that require a join table:
class MyModel < ActiveRecord::Base
has_one :my_score
named_scope :score_greater_than, lambda { |score| {:conditions => ["my_scores.score > ?", score], :joins => :my_score} }
end
class MyScore < ActiveRecord::Base
belongs_to :my_model
end
>>> lo = MyModel.create :name => "Bob", :email => "bob1@berkeley.edu"
>>> hi = MyModel.create :name => "Bob", :email => "bob1@berkeley.edu"
>>> MyScore.create :my_model => lo, :score => 0.0
>>> MyScore.create :my_model => hi, :score => 10.0
>>> MyModel.sql_filter_chain(MyModel.bobs, MyModel.edu_emails, MyModel.score_greater_than(1.0)) == [hi]
=> true
Copyright (c) 2011 [Identified Inc.], released under the MIT license