How to write query option when using gem ruby-firebase (memorial)

I got stuck when linking Ruby on Rails and Firebase, and even if I googled, no information came out, so it also serves as a memorandum.

It is a gem ruby-firebase that is convenient when using Realtime Databa of Firebase, but it is simple and easy to use, but there are few documents and there are not many people using this pattern, so the usage method is not detailed. It is.

I think it's OK if you can see Git of ruby-firebase for basic usage. https://github.com/oscardelben/firebase-ruby

On top of that, the problem was the existence of the query Option.

Originally, there are few queries in Realtime Database and it is not very convenient to use, so it seems that Firestore is better for those who want to use it firmly (I have never used it), but this time I used Realtime Database because it was specified.

Please see the official documentation for how to use Firebase queries. https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-ordered-data

It seems to be a normal usage to pass orderBy or equalTo as a parameter like a URL. What caught me in this area is that the specification such as equalTo is always used as a set with orderBy. It's natural to specify the key with orderBy and the value with equalTo, but it took time to understand--;

So, it is a method to use this with ruby-firebase, but from the conclusion, it is the following entry method.

result = firebase.get("example_dir/", {orderBy: '"id"', equalTo: 100}).body

firebase.get () is used normally, and the hierarchy is specified by example_dir. The hash after the comma is the query, but the important thing here is how to specify the value of orderBy.

Be sure to enclose the value in double quotation marks and then in single quotation marks. This is a way to express it on Ruby because it is necessary to enclose it in double quotation marks when passing it as a parameter when using Firebase.

If this is not specified, the following error will be displayed and the message will be rejected.

orderBy must be a valid JSON encoded path

It took me a long time to find out, so I hope fewer people suffer from the same error.

Recommended Posts

How to write query option when using gem ruby-firebase (memorial)
[Rails] How to write when making a subquery
How to get the query string to actually issue when using PreparedStatement with JDBC
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to add characters to display when using link_to method
How to write to apply gem Pagy (pagination) to an array
[Rails] How to install a decorator using gem draper
How to dynamically write iterative test cases using test / unit (Test :: Unit)
How to test including images when using ActiveStorage and Faker
How to set environment variables when using Payjp with Rails
How to write good code
How to write java comments
[Refactoring] How to write routing
[Note] How to write Dockerfile/docker-compose.yml
How to write and notes when migrating from VB to JAVA
How to exclude auto-generated from Jacoco coverage when using Lombok
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
How to write Rails routing
How to authorize using graphql-ruby
[Java] How to write when passing two or more arguments to super
How to write when installing Amazon Corretto 8 on CentOS 8 with Ansible.
How to solve the unknown error when using slf4j in Java
[Rails 5] How to display the password change screen when using devise
How to write the view when Vue is introduced in Rails?
[Rails] How to use gem "devise"
Studying Java # 6 (How to write blocks)
Baseball ball count (how to write)
How to write a ternary operator
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to build CloudStack using Docker
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Android] How to pass images and receive callbacks when sharing using ShareCompat
How to execute a contract using web3j
How to sort a List using Comparator
[Rails] How to upload images using Carrierwave
[Basic] How to write a Dockerfile Self-learning ②
[Java] How to calculate age using LocalDate
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
How to resolve errors when installing Rails 5.1.3
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
How to make asynchronous pagenations using Kaminari
How to write Spring AOP pointcut specifier
[Swift] How to use one option alert
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
[Rails] How to handle data using enum
How to insert icons using Font awesome