Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FunnyQ/d131cf53df8f545a022552a0fd84c050 to your computer and use it in GitHub Desktop.
Save FunnyQ/d131cf53df8f545a022552a0fd84c050 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