Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixes for methods that use raw sql to work properly with scopes that use joins #38

Merged
merged 2 commits into from

6 participants

@raelik

Because the descendants, subtree and subtree.arrange methods use raw sql as part of their conditions, using them with another scope method breaks if that scope uses joins. These commit fix that by adding the table name to the sql. Any other instances I find like this that break joins, I'll commit to this branch.

Blane Dabney added some commits
Blane Dabney Fix descendants and subtree methods to work properly with scopes that…
… use joins by including the table name in the condition sql.
95afdd0
Blane Dabney 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.
5e0757a
@swrobel

Second this!

@chrismealy

This would be great.

@moiristo

please merge this!

@sobrinho

@stefankroes, could you merge this? :)

@stefankroes
Owner

sorry for the long wait. merged

@stefankroes stefankroes reopened this
@stefankroes stefankroes merged commit 5e0757a into stefankroes:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 17, 2010
  1. Fix descendants and subtree methods to work properly with scopes that…

    Blane Dabney authored
    … use joins by including the table name in the condition sql.
Commits on Feb 14, 2011
  1. Fix ordered_by_ancestry and ordered_by_ancestry_and scopes to work pr…

    Blane Dabney authored
    …operly with scopes that use joins by including the table name in the condition sql.
This page is out of date. Refresh to see the latest.
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
View
6 lib/ancestry/instance_methods.rb
@@ -171,7 +171,7 @@ def is_only_child?
# Descendants
def descendant_conditions
- ["#{self.base_class.ancestry_column} like ? or #{self.base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
+ ["#{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", "#{child_ancestry}/%", child_ancestry]
end
def descendants depth_options = {}
@@ -184,7 +184,7 @@ def descendant_ids depth_options = {}
# Subtree
def subtree_conditions
- ["#{self.base_class.primary_key} = ? or #{self.base_class.ancestry_column} like ? or #{self.base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
+ ["#{self.base_class.table_name}.#{self.base_class.primary_key} = ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} like ? or #{self.base_class.table_name}.#{self.base_class.ancestry_column} = ?", self.id, "#{child_ancestry}/%", child_ancestry]
end
def subtree depth_options = {}
@@ -229,4 +229,4 @@ def primary_key_type
@primary_key_type ||= column_for_attribute(self.class.primary_key).type
end
end
-end
+end
Something went wrong with that request. Please try again.