Created
August 17, 2018 19:26
-
-
Save Aeon/274e6ed3e97950231dbe5cf108ac19bd to your computer and use it in GitHub Desktop.
AREL syntax questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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