I have a Submission model
belongs_to :report
has_many :answers
Report Model
has_many :questions
Question Model
has_many :answers # answer has submission_id
Now in rabl template I need output like this
object @submission
attributes :status, :submission_time
child :report do
attribute :id, :name
child :questions do
attribute :id, :content
node(:answers_count) do |question|
question.answers.where("submission_id = ?", @submission.id).count
end
node(:answers) do |question|
question.answers.where("submission_id = ?", @submission.id).collect{ |answer| {:name => answer.name, :id => answer.id}}
end
end
end
This is working perfectly fine but the problem is in answers_count and answers node I am using @submission and because of that index action is not working.
Its fine in show action but index action is creating issues as @submission is not available in index action.
PS: Once I fix the issue I will move the big query from view to model. I know that, Just for simplicity I have pasted that inline.