I have these models:
#Poll
class Poll < ApplicationRecord
has_many :questions
end
#Question
class Question < ApplicationRecord
belongs_to :poll
end
And the Question model has a boolean field named "hide", when is true, the question does not show in the API's JSON.
Then, I wanna send in a JSON with Grape gem, all polls, with all questions for that poll, something like this:
{Polls =>
{id: 1, name: "poll_name", questions => {
{id: 1, question: "question 1"}}},
{id: 2, name: "poll_name", questions => {
{id: 2, question: "question 2"},
{id: 3, question: "question 3"}}
}
But when I
#api.rb
module AccountStory
class API < ::Grape::API
version 'v1', using: :path
format :json
prefix :api
#api/v1/account_stories
resource :account_stories do
desc 'Return list of polls'
get do
Poll.all.each do |poll|
{"Poll" => {"id" => poll.id, "name" => poll.name, "questions" => poll.questions}}
end
end
end
end
end
But when I execute the server, it return this:
0
id 1
uuid "5b65d39ea89db96a8ab89fd3"
name "Poll 1"
created_at "2018-08-04T16:26:06.218Z"
updated_at "2018-08-04T16:26:06.218Z"
I have in the database only 1 poll, but the script does not get me the questions in that poll.