Ruby Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
      • Ruby on Rails
    • Create your agent
    • Troubleshooting
    • Migrating legacy agents
      • Pre-requisites
      • Recommendations
      • Migration steps
      • Code transformations
        • API Charts
        • Live Queries
        • Smart Charts
        • Route overrides
        • Smart Actions
        • Smart Fields
        • Smart Relationships
        • Smart Segments
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • ActiveRecord
        • Polymorphic relationships
      • Mongoid
    • Write your own
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page

Was this helpful?

  1. Data Sources
  2. Provided data sources

ActiveRecord

PreviousProvided data sourcesNextPolymorphic relationships

Last updated 5 months ago

Was this helpful?

This is the official documentation of the agent_ruby Ruby agent.

The ActiveRecord data source allows importing collections from all models class that extends the abstract class ActiveRecord::Base.

To make everything work as expected, you need to install the gem forest_admin_datasource_active_record.

Note that:

  • ActiveRecord relationships will be respected

def self.setup!
  ForestAdminDatasourceActiveRecord::Datasource.new(
    {
      'adapter' => ENV['DB_ADAPTER'],
      'host' => ENV['DB_HOST'],
      'username' => ENV['DB_USERNAME'],
      'password' => ENV['DB_PASSWORD'],
      'database' => ENV['DB_DATABASE'],
    }
  )
end

Enable support of live queries

By enabling this feature, users with the required permission level can create Live Query components (, and ), allowing them to create more sophisticated requests to your database, by leveraging the underlying query language, SQL in this case.

You can enable this feature by setting a connection name (works as an identifier) when creating your datasource. This connection name will be reflected on the UI when configuring a Live Query component, it should have a clear meaning for your Forest users.

  • If a string is provided (e.g., main_database), ForestAdmin will bind it to the primary database as defined in your Ruby on Rails configuration.

  • If a hash is provided, it should be a mapping of {"primary" => "main_database"}.

def self.setup!
  @create_agent = ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(
    ForestAdminDatasourceActiveRecord::Datasource.new(
      {
        'adapter' => ENV['DB_ADAPTER'],
        'host' => ENV['DB_HOST'],
        'username' => ENV['DB_USERNAME'],
        'password' => ENV['DB_PASSWORD'],
        'database' => ENV['DB_DATABASE'],
      },
      live_query_connections: 'main_database'
    )
  )
  customize
  @create_agent.build
end

Multi databases

ForestAdminDatasourceActiveRecord::Datasource.new(
  {
    'adapter' => ENV['DB_ADAPTER'],
    'host' => ENV['DB_HOST'],
    'username' => ENV['DB_USERNAME'],
    'password' => ENV['DB_PASSWORD'],
    'database' => ENV['DB_DATABASE'],
  },
  live_query_connections: {
    'primary' => 'main_database',
    'primary_replica' => 'replica_database'
  }
)

If you are working with , here is an example of how you can configure live queries to target specific connections for each database :

charts ↗
analytics charts ↗
segments ↗
multiple databases ↗