Skip to content

Instantly share code, notes, and snippets.

@frahugo
Last active August 31, 2020 10:28
Show Gist options
  • Save frahugo/8ceb262e929d4c0a9575 to your computer and use it in GitHub Desktop.
Save frahugo/8ceb262e929d4c0a9575 to your computer and use it in GitHub Desktop.
Ransack with JSONB query
class Registration < ActiveRecord::Base
scope :nhb_consent_eq, -> (enum = "yes") {
where("document @> ?", { nhb_consent: { value: enum } }.to_json )
}
private
def self.ransackable_scopes(auth_object = nil)
%i(nhb_consent_eq)
end
end
> Registration.search(nhb_consent_eq: "yes").result.to_sql
SELECT "registrations".* FROM "registrations" WHERE (document @> '{"nhb_consent":{"value":"yes"}}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment