Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descendants & Ancestors not working #245

Closed
joshRpowell opened this issue Dec 8, 2016 · 5 comments
Closed

Descendants & Ancestors not working #245

joshRpowell opened this issue Dec 8, 2016 · 5 comments

Comments

@joshRpowell
Copy link
Contributor

My configuration:

closure_tree (6.2.0)

class Agent < ActiveRecord::Base
  has_closure_tree name_column: 'full_name'
end

I'm getting my hierarchies from a 3rd party API. The API responds with the children of a given parent. I assign the parent_id on the children as follows:

irb(main):044:0> children = client.hierarchies(parent.id)
=> [#<AgenciesHq::Advisor:0x007f1b2c64b4c0 @raw_data={"id"=>2213632, "url"=>"/advisors/2213632/hierarchies/234074/edit", "name"=>"Sheryl Armstrong", "type"=>"Advisor", "details"=>[{"id"=>144949, "name"=>"NASB", "url"=>"/firms/144949"}, {"name"=>"Level: AG 3"}]}>]
irb(main):045:0> Agent.where(id: children.map(&:id)).update_all(parent_id: parent.id)
  SQL (3.4ms)  UPDATE "agents" SET "parent_id" = 1803147 WHERE "agents"."deleted_at" IS NULL AND "agents"."id" = 2213632
=> 1

When I walk through the tree agent.ancestors returns nil. However, the parent child relationship is established. I can look down, but I can't go up:

irb(main):038:0> agent
=> #<Agent id: 2213632, full_name: "Sheryl Armstrong", parent_id: 1803147>
irb(main):039:0> agent.child?
=> true
irb(main):040:0> agent.parent
  Agent Load (2.4ms)  SELECT "agents".* FROM "agents" WHERE "agents"."deleted_at" IS NULL AND "agents"."id" IN (1803147)
=> #<Agent id: 1803147, full_name: "Lolita Taylor", parent_id: 1805126>
irb(main):041:0> agent.ancestors
  Agent Load (2.4ms)  SELECT "agents".* FROM "agents" INNER JOIN "agent_hierarchies" ON "agents"."id" = "agent_hierarchies"."ancestor_id" WHERE "agents"."deleted_at" IS NULL AND "agent_hierarchies"."descendant_id" = $1 AND ("agents"."id" != 2213632)  ORDER BY "agent_hierarchies".generations asc  [["descendant_id", 2213632]]
=> #<ActiveRecord::AssociationRelation []>
irb(main):042:0> agent.parent.descendants
  Agent Load (3.6ms)  SELECT "agents".* FROM "agents" INNER JOIN "agent_hierarchies" ON "agents"."id" = "agent_hierarchies"."descendant_id" WHERE "agents"."deleted_at" IS NULL AND "agent_hierarchies"."ancestor_id" = $1 AND ("agents"."id" != 1803147)  ORDER BY "agent_hierarchies".generations asc  [["ancestor_id", 1803147]]
=> #<ActiveRecord::AssociationRelation []>
irb(main):043:0> agent.parent.children
  Agent Load (4.3ms)  SELECT "agents".* FROM "agents" WHERE "agents"."deleted_at" IS NULL AND "agents"."parent_id" IN (1803147)
=> #<ActiveRecord::Associations::CollectionProxy [#<Agent id: 2213632, full_name: "Sheryl Armstrong", parent_id: 1803147>

I tried to go about it the opposite direction by adding the child to the parent:

irb(main):051:0> agent.parent.add_child agent
   (2.0ms)  BEGIN
  Agent Exists (2.3ms)  SELECT  1 AS one FROM "agents" WHERE ("agents"."email" = '[email protected]' AND "agents"."deleted_at" IS NULL AND "agents"."id" != 2213632) LIMIT 1
   (1.9ms)  COMMIT
=> #<Agent id: 2213632, full_name: "Sheryl Armstrong", parent_id: 1803147>
irb(main):052:0> agent.ancestors
  Agent Load (2.8ms)  SELECT "agents".* FROM "agents" INNER JOIN "agent_hierarchies" ON "agents"."id" = "agent_hierarchies"."ancestor_id" WHERE "agents"."deleted_at" IS NULL AND "agent_hierarchies"."descendant_id" = $1 AND ("agents"."id" != 2213632)  ORDER BY "agent_hierarchies".generations asc  [["descendant_id", 2213632]]
=> #<ActiveRecord::AssociationRelation []>

This is my first goaround with colsure_tree.

Is there something I'm missing?

Is there a remedy that will get this to work correctly? Reset the hierarchy?

@mceachen
Copy link
Collaborator

mceachen commented Dec 9, 2016 via email

@joshRpowell
Copy link
Contributor Author

@mceachen Thanks for the quick response. I'll give it a try and let you know.

@joshRpowell
Copy link
Contributor Author

same issue as #197 using update_all

@mceachen
Copy link
Collaborator

mceachen commented Dec 9, 2016 via email

@joshRpowell
Copy link
Contributor Author

Excellent! I'll give it a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants