1

I have a database being hosted in development environment and production environment. I am writing an API which based on a parameter in request will write to table in either development or production DB. I have put both entries in by database.yml file :

development:   
  adapter: mysql2   
  database: db1  
  username: root  
  password:  
  timeout: 5000   
  host: a.b.c.d   
  pool: 5  
  port: 1234

production: 
  adapter: mysql2 
  database: db1 
  username: root  
  password:  
  timeout: 5000  
  host: a.b.c.e  
  pool: 5  
  port: 1234

This is my active record :

class table1 < ActiveRecord::Base  
  self.table_name = 'table1' 
end

How do i write to different environments based on a request parameter ?

kronn
  • 925
  • 11
  • 31
NooB8374
  • 99
  • 12
  • do you have an app running in each environment? why not write your API to post to the different apps depending on said parameter? – Taryn East Oct 04 '16 at 22:33
  • No. App is running in only one environment. I want to push it first to development environment and test it out. And then call it for production database. – NooB8374 Oct 04 '16 at 22:34
  • 1
    You don't. Configuration in rails is usually read when the rails server boots. While you can have [multiple database connections](http://stackoverflow.com/questions/6122508/connecting-rails-3-1-with-multiple-databases) - what you most likely want is a staging environment (which mirrors the production environment) which is usually done by creating a separate rails app and by using `ENV['DATABASE_URL']`. – max Oct 04 '16 at 22:54

3 Answers3

3

Go with this one. [ActiveRecord Firebird Adapter][1]

[1]: https://github.com/rowland/activerecord-fb-adapter i used it to connect my local rails app with other production server database, don't forget to add models that will help you to access the database through rails app. goodluck, hope it helps.

Somar Melkhem
  • 238
  • 2
  • 10
2

You could use two models for the same table, one connected to the production database and the other connected to the development database.

Lets say your connection parameters are in the variables $production and $development.

You would also have to adapt your code to know which model to use.

class Table1 < ActiveRecord::Base  
 establish_connection($production)
 self.table_name = 'table1' 
end

class Table1test < ActiveRecord::Base  
 establish_connection($development)
 self.table_name = 'table1' 
end
kronn
  • 925
  • 11
  • 31
peter
  • 41,770
  • 5
  • 64
  • 108
1

For my application I use this gem

https://github.com/thiagopradi/octopus

have good documentation and examples.

If you use establish_connection you will have problems with connections.

Goko Gorgiovski
  • 1,364
  • 2
  • 13
  • 20