How to use 'A' Rails app. database from 'B' app

Go To StackoverFlow.com

2

Let's say I've app. 'A' written in Rails3.2, and there is another app. 'B' also Rails, where i've a sqlite3 db, which contains Users[user_d, value] table, what i want is: i want to search some info from app. 'A' using the user_id which is in app. 'B'.

Plz. help

2012-04-04 07:52
by Said Kaldybaev


2

You need define the connection of your session to point on your table B

connection_to_b = ActiveRecord::Base.establish_connection(
  :adapter  => "sqlite3",
  :database => "db/somedatabase.sqlite3"
)

ActiveRecord::SessionStore::Session.connection = connection_to_b.connection

You can define the table you want too :

ActiveRecord::SessionStore::Session.table_name = 'my_session_table'
2012-04-04 07:57
by shingara
what if i want to use sqlite3 instead of mysql, how can i show the path - Said Kaldybaev 2012-04-04 08:17
and also one note, in my case Sessions table is not about the session, it's just separate tabl - Said Kaldybaev 2012-04-04 08:20
You just need define the params you want in your etablish_connection. I do some example. not to your case. I edit my question to fix the table you want too - shingara 2012-04-04 08:22
sorry, i tried it, but no results. my sqlite file db, located in another server, how i can connect to it ? tried to show the full path to :database option, didn't wor - Said Kaldybaev 2012-04-04 10:08
if it is another server, so for sqlite the only way is to mount remote server as sshfs and then link it to you A app. imho - Pavel S 2012-04-04 10:26
pavel, can you show some example - Said Kaldybaev 2012-04-04 10:32
ask on serverfault : - shingara 2012-04-05 08:41


0

If you're using Rails 4 and Authlogic, you can use this solution inside app B to share user sessions and users, assuming you are sharing database connections between the two apps (app A and app B). All of the code in this example goes inside app B.

# app/models/user_session.rb
class UserSession < Authlogic::Base
  ActiveRecord::Base.establish_connection(
      adapter: 'postgresql', # or 'sqlite3' if you prefer
      database: "db/app_a_#{Rails.env}"
  )
  # You may also need / wish for these:
  logout_on_timeout true
  consecutive_failed_logins_limit 10
  authenticate_with User
end

And, you'll need this:

# config/application.rb - bottom of the file, after the final 'end'
ActionDispatch::Session::ActiveRecordStore.session_class = UserSession

In app B, you'll need a User model that is wired up to talk to the users table in app A's database:

# app/models/user.rb
class User < ActiveRecord::Base
  establish_connection "app_a_#{Rails.env}".to_sym

  # ... whatever else you wish to include in your User model
end

And finally, to show you how the final piece of the puzzle fits together, here is a sample database.yml file for app B (note: all of this file goes inside app B):

# config/database.yml
default: &default
  adapter: postgresql # or sqlite3
  encoding: unicode
  host: localhost
  pool: 5
  timeout: 5000

# Database for app B
development:
  <<: *default
  database: db/app_b_development

test:
  <<: *default
  database: db/app_b_test

production:
  <<: *default
  database: db/app_b_production

# App A's database (containing users and sessions)
app_a_development:
  <<: *default
  database: db/app_a_development

app_a_test:
  <<: *default
  database: db/app_a_test

app_a_production:
  <<: *default
  database: db/app_a_production

HTH someone! :)

2016-03-30 21:54
by Dan Laffan
Ads