Skip to content

Instantly share code, notes, and snippets.

@flash-gordon
Created May 4, 2018 15:32
Show Gist options
  • Save flash-gordon/276a5cef6f9646bfb08caa0c40bcbe79 to your computer and use it in GitHub Desktop.
Save flash-gordon/276a5cef6f9646bfb08caa0c40bcbe79 to your computer and use it in GitHub Desktop.
class Templates < ROM::Relation[:sql]
schema do
attribute :possible_resolution_ids, Types::PG::Array('bigint')
associations do
has_many :resolutions,
as: :possible_resolutions,
relation: :resolutions,
view: :for_possible_resolutions_in_templates,
foreign_key: :template_id,
override: true
end
end
end
class Resolutions < ROM::Relation[:sql]
def for_possible_resolutions_in_templates(_assoc, templates)
template_id = templates.pluck(:template_id)[0]
select_append { int::cast(`'#{ template_id }'`, 'bigint').as(:template_id) }.
where(resolution_id: templates.pluck(:possible_resolution_ids).flatten(1))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment