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