我使用Ruby on Rails并遇到了一个问题,我需要向JSON渲染MySQL查询。我已尝试Google解决此问题并找到了as_json
方法,但该方法并不完全有效。 as_json
方法不会呈现子对象,即使我尝试在单独的调用中呈现sub_object(IE @item[:sub_item] = item.sub_item.as_json
仍然会在到达视图时导致sub_item为nil)
@initial_posts.push({
:attempted => post.attempted,
:body => post.body,
:correct => post.correct,
:course_id => post.course_id,
:created_at => post.created_at,
:entity_type => post.entity_type,
:file => post.file,
:flagged => post.flagged,
:global => post.global,
:id => post.id,
:incorrect => post.incorrect,
:is_link => post.is_link,
:removed => post.removed,
:subscribed => post.subscribed,
:title => post.title,
:updated_at => post.updated_at,
:upvotes => post.upvotes,
:user_id => post.user_id,
:uuid => post.uuid,
:views => post.views,
:voted => post.voted,
:comment => [],
:quiz_choice => [],
:user => {
:UUID => post.user.UUID,
:account_type => post.user.account_type,
:created_at => post.user.created_at,
:email => post.user.email,
:first_name => post.user.first_name,
:id => post.user.id,
:image => post.user.image,
:image_content_type => post.user.image_content_type,
:image_file_name => post.user.image_file_name,
:image_file_size => post.user.image_file_size,
:image_updated_at => post.user.image_updated_at,
:last_name => post.user.last_name,
:picture => post.user.picture,
:rank => post.user.rank,
:reputation => post.user.reputation,
:updated_at => post.user.updated_at,
:upvotes => post.user.upvotes,
:username => post.user.username
}
})
post.comment.each do |comment|
@initial_posts.last[:comment].push({
:comment => comment.comment,
:course_id => comment.course_id,
:created_at => comment.created_at,
:id => comment.id,
:post_id => comment.post_id,
:updated_at => comment.updated_at,
:upvote => comment.upvote,
:user_id => comment.user_id,
:user => {
:UUID => comment.user.UUID,
:account_type => comment.user.account_type,
:created_at => comment.user.created_at,
:email => comment.user.email,
:first_name => comment.user.first_name,
:id => comment.user.id,
:image => comment.user.image,
:image_content_type => comment.user.image_content_type,
:image_file_name => comment.user.image_file_name,
:image_file_size => comment.user.image_file_size,
:image_updated_at => comment.user.image_updated_at,
:last_name => comment.user.last_name,
:picture => comment.user.picture,
:rank => comment.user.rank,
:reputation => comment.user.reputation,
:updated_at => comment.user.updated_at,
:upvotes => comment.user.upvotes,
:username => comment.user.username
}
})
end
post.quiz_choice.each do |quiz_choice|
@initial_posts.last[:quiz_choice].push({
:choice => quiz_choice.choice,
:created_at => quiz_choice.created_at,
:id => quiz_choice.id,
:is_answer => quiz_choice.is_answer,
:post_id => quiz_choice.post_id,
:updated_at => quiz_choice.updated_at
})
end
如果模型或迁移在任何时候发生变化,这显然会成为一个问题。我怎么能用更少,希望更便携的代码来写这个呢?
答案 0 :(得分:2)
如果我正确理解了您的问题,我认为to_json
是您正在寻找的方法。这种方法也可以渲染相关对象,例如:
item.to_json(include: :sub_items)
它将生成:
{ id: item_id, attr1: item_attr1_value, sub_items: [{ id: 2, attr2: value2, ... }, ...]