reason for with_ancestor method using map
ggLee6352 opened this issue · 2 comments
ggLee6352 commented
# lib/closure_tree/finders.rb
def with_ancestor(*ancestors)
ancestor_ids = ancestors.map { |ea| ea.is_a?(ActiveRecord::Base) ? ea._ct_id : ea }
scope = ancestor_ids.blank? ? all : joins(:ancestor_hierarchies).
where("#{_ct.hierarchy_table_name}.ancestor_id" => ancestor_ids).
where("#{_ct.hierarchy_table_name}.generations > 0").
readonly(false)
_ct.scope_with_order(scope)
enddef with_ancestor(*ancestors)
scope = ancestor_ids.blank? ? all : joins(:ancestor_hierarchies).
where("#{_ct.hierarchy_table_name}.ancestor_id" => ancestor_ids).
where("#{_ct.hierarchy_table_name}.generations > 0").
readonly(false)
_ct.scope_with_order(scope)
endseem to be the same
mhazim2 commented
@ggLee6352 map was used to process *ancestors arguments if given any to filter out the results that is descendants of given ancestors. If no argument passed, then will use all scope instead
Model.with_ancestor(1, 2).to_sql
=> SELECT "models".* FROM "models" INNER JOIN "model_hierarchies" ON "model_hierarchies"."descendant_id" = "models"."id" WHERE "model_hierarchies"."ancestor_id" IN (1, 2) AND (model_hierarchies.generations > 0)
Model.with_ancestor([1, 2]).to_sql
=> SELECT "models".* FROM "models" INNER JOIN "model_hierarchies" ON "model_hierarchies"."descendant_id" = "models"."id" WHERE "model_hierarchies"."ancestor_id" IN (1, 2) AND (model_hierarchies.generations > 0)
Model.with_ancestor.to_sql
=> SELECT "models".* FROM "models"Your example by removing ancestors.map { ... } will raise error caused ancestor_ids was not defined since with_ancestor method was a class method not instance method (with_ancestor defined under ClassMethods module)
kbrock commented
Is this still an issue? Can it be closed?