Skip to content

Instantly share code, notes, and snippets.

@fespinoza
Created September 22, 2016 06:49
Show Gist options
  • Save fespinoza/d5ceeb6ab3555ae7357119db8e6b81d0 to your computer and use it in GitHub Desktop.
Save fespinoza/d5ceeb6ab3555ae7357119db8e6b81d0 to your computer and use it in GitHub Desktop.
Ruby 2.3 heredocs
# frozen_string_literal: true
module Organization
class Group
# belongs_to :parent, class_name: 'Organization::Group'
# has_many :children, class_name: 'Organization::Group',
# foreign_key: :parent_id
# has_many :group_memberships, dependent: :destroy
# has_many :users, through: :group_memberships
RECURSIVE_UP = <<SQL.freeze
WITH RECURSIVE group_tree AS (
SELECT g.*
FROM organization_groups g
WHERE g.id IN (?)
UNION
SELECT g.*
FROM organization_groups g
JOIN group_tree t
ON g.id = t.parent_id
)
SELECT * FROM group_tree t;
SQL
RECURSIVE_DOWN = <<~HEREDOC
WITH RECURSIVE group_tree AS (
SELECT g.*
FROM organization_groups g
WHERE g.id IN (?)
UNION
SELECT g.*
FROM organization_groups g
JOIN group_tree t
ON g.id = t.parent_id
)
SELECT * FROM group_tree t;
HEREDOC
end
end
@tataihono
Copy link

This helped me figure out how to freeze heredocs. Thanks!

@stratigos
Copy link

Also thx for demo on how/where to freeze a heredoc!

@subelsky
Copy link

👏 thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment