All threads / Working with ActiveRecord and Google Cloud Firestore
Ask A Question


You’re not receiving notifications from this thread.

Working with ActiveRecord and Google Cloud Firestore

Jordan T-H asked in Rails

I'm pretty new to Rails, and I was wondering what the "right" way of architecting a small app would be given the following scenario. Users create Rooms, then from there add to them a number of references ("Documents") to track that are pulled in from Google Cloud Firestore. Rooms are basically table displays for these collections of Documents. Documents can be in more than one Room. I have a service object to do stuff with Cloud Firestore.

I see two ways of doing this:

The first is to simply store each Document's id in a serialized text attribute (or a store) on a given Room. This seems inelegant and non-Rails-y, especially when I think about the kind of query I'd need to construct to analyze documents across all rooms. Or maybe not? What makes this interesting is that it is simple. I can just query Firestore for the relevant information each time I want to display it. Right now, there's really only one attribute in Firestore I'm interested in, so I could even directly store the filtered hash from the Firestore collection in this attribute as an ActiveRecord::Store, do a comparison every time a Room is accessed (I think the Firestore gem might even do this caching), then update it from Firestore.

The second is to add a Document model for each document. Instead of working with Document ids in arrays from a given Room, I'd have a table for Documents, and a join table for the connections between Documents and Rooms. In this situation, I'd like some kind of caching or something, where any time I get information about a Document back from a query I update it in the database, or some way of maintaining this isomorphism while preserving how easy it is to work with ActiveRecord. This app is read-only for now, and it's not catastrophic if something is a few minutes out of date.

Has anyone tackled a situation like this, working from two databases and trying to reconcile them? I'm tempted to just start with the first version because it's so simple, then grow out to the second, unless someone has some enlightened ideas. Thank you in advance for reading.

Not sure what the etiquette is for answering or following up one's own question, but I ended up modeling this, because I realized I need some pretty complicated and persistent logic for each document's lifecycle, a subject of another question.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 66,029+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.