Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5996161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:03:50+00:00 2026-05-23T00:03:50+00:00

class Service < ActiveRecord::Base establish_connection( :adapter => mysql, :host => myip, :username => myusername,

  • 0
class Service < ActiveRecord::Base

establish_connection(
  :adapter  => "mysql",
  :host     => "myip",
  :username => "myusername",
  :password => "mypassword",
  :database => "mydatabase"
)

end

This works

Service.all #connects to mydatabase

But i need something like that.

Service.use(mydatabase1).all #connects to mydatabase1
Service.use(mydatabase2).all #connects to mydatabase2

How can i achieve this?

Update

Database names are dynamic. I want Service model to connect database dynamically.
When i type Service.use(weeweweaszxc).all it has to use weeweweaszxc database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:03:50+00:00Added an answer on May 23, 2026 at 12:03 am

    Try taking a look as this question over here.
    How to best handle per-Model database connections with ActiveRecord?

    They define the databases in the database.yml file like normal and call this in the model:

    class AnotherDatabase < ActiveRecord::Base
      self.abstract_class = true
      establish_connection "anotherbase_#{RAILS_ENV}"
    end
    

    Used info from Priit’s answer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: class Service < ActiveRecord::Base has_and_belongs_to_many :staffs and class Staff < ActiveRecord::Base has_and_belongs_to_many
I have a the following classes class Service < ActiveRecord::Base has_many :incidents end class
I've got the following relationships in my model: class Show < ActiveRecord::Base has_many :service_shows
I have a model class ServiceRequest < ActiveRecord::Base has_many :services accepts_nested_attributes_for :services ... end
class Person < ActiveRecord::Base validates :terms_of_service, :acceptance => true end In the above, what
I have an active record class class Service_Catalogue < ActiveRecord::Base set_table_name service_catalogue set_primary_key myKey
Very simple example: Model: require 'inventory' class CustomerOrder < ActiveRecord::Base validates_presence_of :name validate :must_have_at_least_one_item,
i'm trying to do something like class Event < ActiveRecord::Base has_many :links, :dependent =>
Assume the following models: class Order < ActiveRecord::Base belongs_to :customer has_many :line_items, :dependent =>
I have the following models: require 'books_projects.rb' class Project < ActiveRecord::Base has_many :book_to_projects has_many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.