Skip to content

Instantly share code, notes, and snippets.

@havran
Created April 8, 2016 09:00
Show Gist options
  • Save havran/054c99bca3832bd9bb8019b0e56a141e to your computer and use it in GitHub Desktop.
Save havran/054c99bca3832bd9bb8019b0e56a141e to your computer and use it in GitHub Desktop.
GEM pluck to hash example
if @records
posts = @records.map{|p| p.post_id}
posts_extra_data = Message::Post
.joins(
'LEFT JOIN "message_webs" ON "message_webs"."post_id" = "message_posts"."id"',
'LEFT JOIN "source_sources" ON "source_sources"."id" = "message_posts"."source_source_id"',
'LEFT JOIN "source_sections" ON "source_sections"."id" = "message_posts"."source_section_id"'
)
.where(id: posts)
.pluck_h(
:id,
'message_webs.link_url AS link_url',
'source_sources.id AS source_id',
'source_sources.title AS source_title',
'source_sections.id AS section_id',
'source_sections.title AS section_title'
)
@records_web_links = {}; posts_extra_data.each{|p| @records_web_links[p['id']] = p['link_url']}
@records_sources = {}; posts_extra_data.select{|p| !p['source_id'].nil?}.each{|p| @records_sources[p['source_id']] = p['source_title']}
@records_sections = {}; posts_extra_data.select{|p| !p['section_id'].nil?}.each{|p| @records_sections[p['section_id']] = p['section_title']}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment