Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Split out left outer join creation into its own method, allowing yo…

…u to use it in your own named scopes.
  • Loading branch information...
commit 8563a4d5a70fddacefed1dfc4d0c38f67101e06f 1 parent 1cb9ecb
@binarylogic authored
View
1  CHANGELOG.rdoc
@@ -1,6 +1,7 @@
== 2.0.1
* Allow the chaining of conditions off of a search object. Ex: search.username_like("bjohnson").age_gt(20).all
+* Split out left outer join creation into its own method, allowing you to use it in your own named scopes.
== 2.0.0
View
16 README.rdoc
@@ -103,13 +103,23 @@ Obviously, only do this if you want to actually use the included objects.
== Make searching and ordering data in your application trivial
-The above is great, but what about tying all of this in with a search form in your application? Just do this...
+The above is great, but what about tying all of this in with a search form in your application? What would be really nice is if we could use an object that represented a single search. Like this...
- User.search(:username_like => "bjohnson", :age_less_than => 20)
+ search = User.search(:username_like => "bjohnson", :age_less_than => 20)
+ search.all
The above is equivalent to:
- User.username_like("bjohnson").age_less_than(20)
+ User.username_like("bjohnson").age_less_than(20).all
+
+You can set, read, and chain conditions off of your search too:
+
+ search.username_like => "bjohnson"
+ search.age_gt = 2 => 2
+ search.id_gt(10).email_begins_with("bjohnson") => <#Searchlogic::Search...>
+ search.all => An array of users
+ search.count => integer
+ # .. etc
All that the search method does is chain named scopes together for you. What's so great about that? It keeps your controllers extremely simple:
View
19 lib/searchlogic/named_scopes/associations.rb
@@ -135,9 +135,24 @@ def association_condition_options(association_name, association_condition, args)
#
# The code below was extracted out of AR's add_joins! method and then modified.
def add_left_outer_joins(options, association)
- join = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, association.name, nil).join_associations.collect { |assoc| assoc.association_join }.join.strip
+ joins = left_outer_joins(association.name)
options[:joins] ||= []
- options[:joins].unshift(join)
+ options[:joins] = joins + options[:joins]
+ end
+
+ # Leverages ActiveRecord's JoinDependency class to create a left outer join. Searchlogic uses left outer joins so that
+ # records with no associations are included in the result when the association is optional. You can use this method
+ # internally when creating your own named scopes that need joins. You need to do this because then ActiveRecord will
+ # remove any duplicate joins for you when you chain named scopes that require the same join. If you are using a
+ # LEFT OUTER JOIN and an INNER JOIN, ActiveRecord will add both to the query, causing SQL errors.
+ #
+ # Bottom line, this is convenience method that you can use when creating your own named scopes. Ex:
+ #
+ # named_scope :orders_line_items_price_expensive, :joins => left_out_joins(:orders => :line_items), :conditions => "line_items.price > 100"
+ #
+ # Now your joins are consistent with Searchlogic allowing you to avoid SQL errors with duplicate joins.
+ def left_outer_joins(association_name)
+ ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, association_name, nil).join_associations.collect { |assoc| assoc.association_join.strip }
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.