Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RafaelMCarvalho/51924ff19dfce4942f5c946a4927d296 to your computer and use it in GitHub Desktop.
Save RafaelMCarvalho/51924ff19dfce4942f5c946a4927d296 to your computer and use it in GitHub Desktop.
Ransack with JSONB query for array values (or condition)
class Edition < ActiveRecord::Base
scope :distances_any, -> (*distances) {
where("distances ?| array[:values]", values: distances)
}
private
def self.ransackable_scopes(auth_object = nil)
%i(distances_any)
end
end
> Edition.ransack(distances_any: ['short', 'long']).result.to_sql
SELECT "editions".* FROM "editions" WHERE (distances ?| array['short','long'])
@RafaelMCarvalho
Copy link
Author

Using PostgreSQL and jsonb with Ruby on Rails
http://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment