Skip to content
Browse files

* Fixed bug when an associations named scope joins is a string or an …

…array of strings, the joins we add in automatically should also be a string, not a symbol.
  • Loading branch information...
1 parent f884a57 commit 0c52a13b846fee85782454f39e226077ea00e830 @binarylogic committed
View
4 CHANGELOG.rdoc
@@ -1,3 +1,7 @@
+== 2.2.3 released 2009-07-31
+
+* Fixed bug when an associations named scope joins is a string or an array of strings, the joins we add in automatically should also be a string, not a symbol.
+
== 2.2.2 released 2009-07-31
* Fix bug to give priority to local columns.
View
16 lib/searchlogic/named_scopes/association_conditions.rb
@@ -61,8 +61,7 @@ def association_condition_options(association_name, association_condition, args)
# The underlying condition doesn't require any parameters, so let's just create a simple
# named scope that is based on a hash.
options = scope.scope(:find)
- options.delete(:readonly)
- options[:joins] = options[:joins].blank? ? association.name : {association.name => options[:joins]}
+ prepare_named_scope_options(options, association)
options
else
# The underlying condition requires parameters, let's match the parameters it requires
@@ -88,13 +87,22 @@ def association_condition_options(association_name, association_condition, args)
searchlogic_lambda(:#{arg_type}) { |#{proc_args.join(",")}|
scope = association.klass.send(association_condition, #{proc_args.join(",")})
options = scope ? scope.scope(:find) : {}
- options.delete(:readonly)
- options[:joins] = options[:joins].blank? ? association.name : {association.name => options[:joins]}
+ prepare_named_scope_options(options, association)
options
}
end_eval
end
end
+
+ def prepare_named_scope_options(options, association)
+ options.delete(:readonly)
+
+ if options[:joins].is_a?(String) || array_of_strings?(options[:joins])
+ options[:joins] = [inner_joins(association.name), options[:joins]].flatten
+ else
+ options[:joins] = options[:joins].blank? ? association.name : {association.name => options[:joins]}
+ end
+ end
end
end
end
View
5 spec/named_scopes/association_conditions_spec.rb
@@ -125,4 +125,9 @@
order = user.orders.create(:total => 20, :taxes => 3)
Company.users_orders_taxes_lt(50).ascend_by_users_orders_total.all(:include => {:users => :orders}).should == Company.all
end
+
+ it "should automatically add string joins if the association condition is using strings" do
+ User.named_scope(:orders_big_id, :joins => User.inner_joins(:orders))
+ Company.users_orders_big_id.proxy_options.should == {:joins=>[" INNER JOIN \"users\" ON users.company_id = companies.id ", " INNER JOIN \"orders\" ON orders.user_id = users.id "]}
+ end
end

0 comments on commit 0c52a13

Please sign in to comment.
Something went wrong with that request. Please try again.