Patricio S
Patricio S

Reputation: 2130

Nesting associations in JBuilder with Rails

I have the following json jbuilder:

json.question_cluster @question_cluster

json.questions @question_cluster.questions do |question|
  json.id question.id
  json.title question.title
  json.required question.required
  json.has_other question.has_other
  json.position question.position
  json.options question.options do |option|
    json.id option.id
    json.label option.label
    json.value option.value
    json.position option.position
    json.go_page option.go_page
  end
end

Which generates the following response in my app:

nested_json

The problem is that I want to put questions into question_cluster, but @question_cluster is a single object, so I can't use do end (it throws an error), what can I do in this case?

Upvotes: 0

Views: 1806

Answers (1)

Sergei Stralenia
Sergei Stralenia

Reputation: 865

Did you try this way?

json.question_cluster do
  json.(@question_cluster)
  json.questions @question_cluster.questions do |question|
    json.id question.id
    json.title question.title
    json.required question.required
    json.has_other question.has_other
    json.position question.position
    json.options question.options do |option|
      json.id option.id
      json.label option.label
      json.value option.value
      json.position option.position
      json.go_page option.go_page
    end
  end
end

Upvotes: 2

Related Questions