Does Ruby on Rails Work With Firebase?
Rails and Firebase can work together, but Firebase is primarily a frontend/backend-as-a-service solution, so you're essentially using Rails as an API layer alongside Firebase services.
Quick Facts
How Ruby on Rails Works With Firebase
Rails and Firebase integration requires treating them as complementary services rather than a native partnership. Rails typically handles your application logic, routing, and database models, while Firebase provides real-time database, authentication, and hosting. The common pattern is using Rails as a REST/GraphQL API backend that reads/writes to Firestore or Realtime Database via the Firebase Admin SDK, while frontend code (React, Vue, etc.) connects directly to Firebase for real-time features. Alternatively, you can use Firebase Auth for authentication and have Rails validate tokens server-side. The developer experience involves managing two distinct backend systems—Rails models sync with Firestore collections, requiring careful coordination. Authentication is the smoothest integration point since Firebase tokens can be verified in Rails middleware. The main architectural consideration is data consistency: Rails ORM and Firestore have different paradigms, so you'll need to decide which system owns each data domain.
Best Use Cases
Rails + Firebase Firestore Integration
bundle add firebase-adminrequire 'firebase'
# config/initializers/firebase.rb
Firebase.configure do |config|
config.project_id = Rails.application.credentials.firebase[:project_id]
config.private_key = Rails.application.credentials.firebase[:private_key]
config.client_email = Rails.application.credentials.firebase[:client_email]
end
# app/models/user.rb
class User < ApplicationRecord
def sync_to_firestore
db = Firebase::Admin.firestore
db.doc("users/#{self.id}").set({
name: self.name,
email: self.email,
updated_at: Time.current.to_i
})
end
end
# app/controllers/api/users_controller.rb
class Api::UsersController < ApplicationController
def create
user = User.create(user_params)
user.sync_to_firestore
render json: user
end
endKnown Issues & Gotchas
Firebase Realtime Database and Firestore have different query capabilities than SQL—complex Rails queries don't translate directly
Fix: Map out which data lives in Rails PostgreSQL vs Firestore upfront. Keep transactional data in Rails, use Firestore for denormalized, real-time data.
Firebase Admin SDK authentication requires service account keys; storing these securely in Rails environment is critical
Fix: Use Rails credentials system or environment variables, never commit keys to repo. Consider using Workload Identity if deploying to GCP.
Firestore charges per read/write operation—N+1 queries from Rails can become expensive quickly
Fix: Batch Firestore operations using transaction APIs, implement aggressive caching, design queries to minimize reads.
Firebase hosting and Rails deployment are separate concerns, requiring separate CI/CD pipelines
Fix: Use separate deployment services or manage both through containers. If using Rails for API only, deploy to Cloud Run and host frontend on Firebase Hosting.
Alternatives
- •Supabase + Rails: PostgreSQL-based Firebase alternative with tighter Rails integration via supabase-rb gem
- •Rails + AWS (DynamoDB/Cognito): More traditional separation with AWS SDK, better for enterprise environments
- •Next.js + Firebase: Skip Rails entirely for full-stack JavaScript with Firebase—simpler if you don't need server-side Ruby
Resources
Related Compatibility Guides
Explore more compatibility guides