我想从以下查询中获得响应:
sites = Site.find(request.review_sites).pluck(:id, :location_id, :site_name, :review_url,:created_at)
提供如下输出,
[
[
50,
2,
"google",
"https://search.google.com/local/writereview?placeid=000000001flMRNRVr8YpJWuY",
Thu, 06 Dec 2018 20:18:29 UTC +00:00
],
[
51,
2,
"facebook",
"https://www.facebook.com/biz/reviews/?ref=page_internal",
Thu, 06 Dec 2018 20:18:35 UTC +00:00
]
]
变成如下所示的嵌套散列(稍后我将使用它作为 JSON):
{
:google => {
:id => 50,
:create_at => "2018-12-06T20:18:29.651Z",
:site_name => "google",
:review_url => "https://search.google.com/local/writereview?placeid=00000000001flMRNRVr8YpJWuY",
:location_id => 2
},
:facebook => {
:id => 51,
:create_at => "2018-12-06T20:18:35.639Z",
:site_name => "facebook",
:review_url => "https://www.facebook.com/biz/reviews/?ref=page_internal",
:location_id => 2
}
}
我意识到这是重复的,但我正在努力实施给出的一些答案。我正在努力以编程方式执行此操作,因此将不胜感激。
我有你要找的东西
尝试以下,
sites = Site.find(request.review_sites)
.as_json(only: [:id, :location_id, :site_name, :review_url,:created_at])
.group_by { |x| x['site_name'] }
输出看起来像,
{
'google' => [
{
'id' => 50,
'create_at' => "2018-12-06T20:18:29.651Z",
'site_name' => "google",
'review_url' => "https://search.google.com/local/writereview",
'location_id' => 2
},
{
'id' => 52,
'create_at' => "2018-14-06T20:18:29.651Z",
'site_name' => "google",
'review_url' => "https://search.google.com/local/readerview",
'location_id' => 3
}
],
'facebook' => [
{
'id' => 51,
'create_at' => "2018-12-06T20:18:35.639Z",
'site_name' => "facebook",
'review_url' => "https://www.facebook.com/biz/reviews/?ref=page_internal",
'location_id' => 2
}
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句