gpt4 book ai didi

mysql - 表达这段代码的更简洁的方式是什么?

转载 作者:行者123 更新时间:2023-11-29 07:25:14 24 4
gpt4 key购买 nike

我正在使用 Ruby on Rails,并且遇到了一个问题,我需要将 MySQL 查询呈现为 JSON。我尝试通过 Google 搜索该问题,并找到了 as_json 方法,该方法并不完全有效。即使我尝试在单独的调用中渲染 sub_object,as_json 方法也不会渲染子对象(IE @item[:sub_item] = item.sub_item.as_json 当 sub_item 到达 View 时仍然会导致 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

如果模型或迁移在任何时候发生更改,这显然会成为一个问题。我怎样才能用更少的、希望更便携的代码来编写这个?

最佳答案

如果我正确理解你的问题,我认为 to_json 是你正在寻找的方法。这样的方法也可以渲染相关对象,例如:

item.to_json(include: :sub_items)

它将生成:

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

关于mysql - 表达这段代码的更简洁的方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com