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
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'
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! :)