简体   繁体   中英

Rails JBuilder recursive lookup

I am using Rails 4, and I have some simple models as follow:

class Question < ActiveRecord::Base
  # columns (id, text)
  has_many :answers
end

class Answer < ActiveRecord::Base
  # columns (id, text, next_question_id)
  belongs_to :question
end

You can see that an answer has a next_question_id column, which will be used to look up another question. I want to generate a tree-structure json like this:

{
  "text": "This is question 1",
  "answers": [
    {
      "text": "This is answer a",
      "next_question": {
        "text": "This is question 2",
        "answers": [
          {
            "text": "This is answer c",
            "next_question":
          }
        ]
      }
    },
    {
      "text": "This is answer b",
      "next_question": {
        "text": "This is question 2",
        "answers": [
          {
            "text": "This is answer d",
            "next_question":
          }
        ]
      }
    }
  ]
}

How can I achieve this with JBuilder? I tried the solution here , but I cannot pass the json argument to the helper function.

The standard aproach for rendering trees is using a recursive partial. To implement this you'll first need to add a method to your Answer model like this.

def next_question
  Question.find(next_question_id) if next_question_id  
end

( hint : alternetively you could just set a belongs_to :next_question, class_name: Question association on your Answer model)

Then you create a partial like _question.json.jbuilder that goes like this:

json.(question,:id, :text)
json.answers question.answers do |answer|
  json.(answer, :id, :text)
  json.partial!(:question, question: answer.next_question) if answer.next_question
end

Then in the controller you take the first question in your survey and put it in say the @first_question variable.

And the last thing: in your view you write

json.partial! :question, question: @first_question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM