Skip to content

Instantly share code, notes, and snippets.

@Aeon
Created August 17, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aeon/274e6ed3e97950231dbe5cf108ac19bd to your computer and use it in GitHub Desktop.
Save Aeon/274e6ed3e97950231dbe5cf108ac19bd to your computer and use it in GitHub Desktop.
AREL syntax questions
class Manifest < ApplicationRecord
def self.item_ids(id, keyword=nil, prefix=nil)
cases = Arel::Table.new(:manifest_items)
casequery = cases.where(cases[:manifest_id].eq(id))
if keyword
# "keywords" is an string array column in the database, so I want to generate
# "'#{keyword}' = ANY(keywords)", essentially, with correct quoting/escaping
selectmanager = casequery.and(Arel::Nodes::Quoted.new(keyword).eq_any(cases[:keywords]))
end
if prefix
#
selectmanager = casequery.and(cases[:title].matches("#{prefix}%"))
end
query = casequery.project(cases[:item_id]).distinct.to_sql
return query
self.connection.execute(
query
).pluck(:item_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment