Skip to content
Browse files

Fix ordered_by_ancestry and ordered_by_ancestry_and scopes to work pr…

…operly with scopes that use joins by including the table name in the condition sql.
  • Loading branch information...
1 parent 95afdd0 commit 5e0757a8fe69f6eaf458a6f4a35633bda6d1a854 Blane Dabney committed Feb 14, 2011
Showing with 3 additions and 3 deletions.
  1. +3 −3 lib/ancestry/has_ancestry.rb
View
6 lib/ancestry/has_ancestry.rb
@@ -51,8 +51,8 @@ def has_ancestry options = {}
send scope_method, :descendants_of, lambda { |object| {:conditions => to_node(object).descendant_conditions} }
send scope_method, :subtree_of, lambda { |object| {:conditions => to_node(object).subtree_conditions} }
send scope_method, :siblings_of, lambda { |object| {:conditions => to_node(object).sibling_conditions} }
- send scope_method, :ordered_by_ancestry, :order => "(case when #{ancestry_column} is null then 0 else 1 end), #{ancestry_column}"
- send scope_method, :ordered_by_ancestry_and, lambda { |order| {:order => "(case when #{ancestry_column} is null then 0 else 1 end), #{ancestry_column}, #{order}"} }
+ send scope_method, :ordered_by_ancestry, :order => "(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}"
+ send scope_method, :ordered_by_ancestry_and, lambda { |order| {:order => "(case when #{table_name}.#{ancestry_column} is null then 0 else 1 end), #{table_name}.#{ancestry_column}, #{order}"} }
# Update descendants with new ancestry before save
before_save :update_descendants_with_new_ancestry
@@ -86,4 +86,4 @@ def has_ancestry options = {}
if !respond_to?(:acts_as_tree)
alias_method :acts_as_tree, :has_ancestry
end
-end
+end

0 comments on commit 5e0757a

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