Database push/pull

Hi,

I have two apps which are independent but I now would like to help them talk to each other. Specifically I would like to allow users registering to one site to be able to have an account created at the other automatically.

Both sites use Restful_authentication plugin so the User table is the same structure, so all I would need to pass is name, email and password in some sort of information push.

As you can tell from my terminology this is something I have no experience with so can anybody point me in the direction of a good tutorial, or have you tried something similar?

Thanks,

Dan

You can open a connection to the other DB and save the User there; see the ActiveRecord::Base API doc.

That's probably easier and better than messing with replication. But you'll also want to consider other actions besides new (e.g. when the user updates their account data -- new email, change password, etc.)

FWIW,

Hey Hassan,

Thanks for the pointer, I have been looking at the API and the establish_connection seems like the way to go.

  ActiveRecord::Base.establish_connection(     :adapter => "mysql",     :host => "localhost",     :username => "myuser",     :password => "mypass",     :database => "somedatabase"   )

The databases both exist on the same server but one is mysql and one is postgresql.

It is not clear to me how to implement this though?

I suppose I could try adding it in the User model as a before_create method and pass the params to the other database, but I have no idea what kind of structure that would have.

Any further input would be very much appreciated, but I will keep this thread updated if I manage to figure it out.

Best,

Dan

I'm sure there's more than one way, but here's a suggestion.

You have a User now   class User < ActiveRecord::Base     # whatever   end

Create a   class AssociatedUser < User     establish_connection :associated_site   end

Define associated_site in your database.yml

Then you can simply "clone" your user   a_user = AssociatedUser.new   a_user.attributes= user.attributes   a_user.save

And done :slight_smile:

Caveat: if this is an existing user (already saved in the first app) then you probably want to replace the above with something like   a_user.attributes= user.attributes.except("id") to avoid clobbering someone else's account.

Serving suggestion, write lots of tests before implementing :slight_smile: