-3

I have JSON: JSON Link

Now I need to create parent child relationship. So that I can consume data:

HTTP GET /:tree_id
=> Return the saved structure
HTTP GET /:tree_id/parent/:id
=> Return the list of parents
HTTP GET /:tree_id/child/:id
=> Return the list of childs

I have Couple gems eg. acts_as_tree & ancestry But all the above gems are providing me use parent.children to get the data.

But I nee to consume above API. Can you please help how i should use association and model structure to save and get the data based on above REST request.

John Ross
  • 77
  • 1
  • 1
  • 5
  • What exactly do you mean "I need to create parent child relationship"? What is the output you are trying to create? – jvillian Mar 21 '19 at 06:26
  • HTTP GET /:tree_id/parent/:id, If someone hits this url it, It will return the list of parents IDs Or HTTP GET /:tree_id/child/:id => Return the list of childs – John Ross Mar 21 '19 at 06:30
  • It can be a search function to find Child/Parent nodes – John Ross Mar 21 '19 at 06:33
  • What would be the problem to implement the endpoint performing a request to the datastore, getting all the parents/children and returning them? – Aleksei Matiushkin Mar 21 '19 at 06:34
  • 1
    Please do not link to external sites that host your code. If you have code that is relevant to your question then you must include it directly in your question. – anothermh Mar 21 '19 at 06:34
  • I know that Its a link for Pastbin. And i believe thats completely fine. – John Ross Mar 21 '19 at 06:38
  • I'm confused. If `/:tree_id/parent/:id` is supposed to return a _list_ of parents, what exacty is the `:id` param? – max pleaner Mar 21 '19 at 06:48
  • This is a bit too broad, unfortunately. I advise to add your attempts to make models/controllers, if possible. – max pleaner Mar 21 '19 at 06:49
  • 3
    I can’t visit pastebin from here so I can’t see your example. If you think it’s “completely fine” to leave out the only meaningful and relevant code that would enable me or anyone else to answer your question then I’m not sure why you’d take the time to ask one at all. SO gives you the tools you need to include it in your post; why would you go out of your way to make it harder for people to help you? What benefit do you get from using an external site that beats the drawback of people being unable to see your code and being unable to answer your question? – anothermh Mar 21 '19 at 23:30

1 Answers1

2

How about you create a module like:

module Ancestry

  def self.extended(receiver)
    receiver.class_eval do 

      define_method(:children_ids) do |children_ary=[]|
        receiver[:child].each_with_object(children_ary) do |child, children_ary|
          children_ary << child[:id]
          child.extend(Ancestry).children_ids(children_ary)
        end if receiver[:child].any?
      end

      define_method(:parent_ids) do |parents_ary=[]|
        if receiver[:child].any?
          parents_ary << receiver[:id]
          receiver[:child].each_with_object(parents_ary) do |child, parents_ary|
            child.extend(Ancestry).parent_ids(parents_ary)
          end
        end
      end

      define_method(:node) do |node_id|
        return receiver if receiver[:id] == node_id
        receiver[:child].each do |child|
          child.extend(Ancestry).node(node_id).tap do |x| 
            return x unless x.blank?
          end
        end if receiver[:child]
        {}.extend(Ancestry)
      end

      define_method(:child) do 
        receiver[:child] || []
      end

    end
  end

end

Then extend your hash like this:

data = {
  "id": 1,
  "child": [
    {
      "id": 1847,
      "child": [
        {
          "id": 8078,
          "child": []
        },
        {
          "id": 3380,
          "child": [
            {
              "id": 561,
              "child": []
            },
            {
              "id": 706,
              "child": []
            }
          ]
        }
      ]
    }
  ]
}.with_indifferent_access.extend(Ancestry)

Now you can do:

data.children_ids
 => [1847, 8078, 3380, 561, 706]
data.parent_ids
 => [1, 1847, 3380]
data.node(1847).node(3380).child
 => [{"id"=>561, "child"=>[]}, {"id"=>706, "child"=>[]}]
data.node(9999).node(3380).child
 => []
data.node(1847).node(9999).child
 => []
data.node(1847).node(3380).node(561).child
 => []
jvillian
  • 19,953
  • 5
  • 31
  • 44