2016-01-13 55 views
0

我正在使用Ruby on Rails,並遇到了一個問題,我需要向JSON呈現MySQL查詢。我試圖谷歌的問題,並找到了as_json方法,這不完全工作。 as_json方法不會呈現子對象,即使我嘗試在單獨的調用中呈現子對象時(IE @item[:sub_item] = item.sub_item.as_json仍會導致sub_item在到達視圖時爲零)。什麼是較不詳細的表達此代碼的方式?

@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 

這顯然是一個問題,如果在任何時候改變模型或遷移。我怎樣才能以更少的代碼寫這個,希望更便攜的代碼?

回答

2

如果我正確理解你的問題,我認爲to_json是你正在尋找的方法。這種方法可以使相關的對象也一樣,例如:

item.to_json(include: :sub_items) 

它會生成:

{ id: item_id, attr1: item_attr1_value, sub_items: [{ id: 2, attr2: value2, ... }, ...]