Skip to content

Instantly share code, notes, and snippets.

@gabeodess
Forked from frahugo/gist:8ceb262e929d4c0a9575
Created January 14, 2016 16:51
Show Gist options
  • Save gabeodess/af99b6c4d2aec4fb1c5f to your computer and use it in GitHub Desktop.
Save gabeodess/af99b6c4d2aec4fb1c5f 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