Model Structure :
Comment Model has belongs_to relation
and Resource Model doesn't have any relation.
class Comment < ApplicationRecord
belongs_to :resource
end
Class Resource < ApplicationRecord
#No relation
end
This is my query : (Hope this is not n+1 as I have used includes)
comments = Comment.where(id: [5,6,7]).includes(:resource)
Hash[comments.map { |comment|
[comment.id, comment.resource] if comment.resource
}]
Output that I got is this:
In which 5 and 6 are comment_id and other is the resource _attributes
5=>
#<Resource:0x00007fa9b2a93518
id: 3,
url: "http://url.com",
description: "og:description",
title: "title",
site_name: "og:site_name",
image_url: "http://imageurl.com",
video_url: nil,
created_at: Mon, 28 Oct 2019 07:44:16 UTC +00:00,
updated_at: Mon, 28 Oct 2019 07:44:16 UTC +00:00,
embed_html: nil,
url_hash: "df5ac6d4a1313ecb9949312d0258416a248333f68ae39aa1c7acd818e7d997e7",
user_id: nil>,
6=>
#<Resource:0x00007fa9b2a92fa0
id: 4,
url: "http://url.com",
description: "og:description",
title: "title",
site_name: "og:site_name",
image_url: "http://imageurl.com",
video_url: nil,
created_at: Mon, 28 Oct 2019 07:44:16 UTC +00:00,
updated_at: Mon, 28 Oct 2019 07:44:16 UTC +00:00,
embed_html: nil,
url_hash: "df5ac6d4a1313ecb9949312d0258416a248333f68ae39aa1c7acd818e7d997e7",
user_id: nil>}
But I need more optimized query and output should be like:
5=>
#<Resource:0x00007fa9b2a93518
id: 3,
url: "http://url.com",
description: "og:description",
,
6=>
#<Resource:0x00007fa9b2a92fa0
id: 4,
url: "http://url.com",
description: "og:description"
}