# Database push/pull

**URL:** <https://www.ruby-forum.com/t/database-push-pull/184730>\
**Category:** Rails\
**Created:** [March 7, 2010, 4:36pm UTC](https://www.ruby-forum.com/t/database-push-pull/184730 "2010-03-07T16:36:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [March 7, 2010, 4:36pm UTC](https://www.ruby-forum.com/t/database-push-pull/184730/1 "2010-03-07T16:36:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [March 7, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/database-push-pull/184730/2 "2010-03-07T18:10:37Z")

</div>

On Sun, Mar 7, 2010 at 7:35 AM, DanC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [March 7, 2010, 9:31pm UTC](https://www.ruby-forum.com/t/database-push-pull/184730/3 "2010-03-07T21:31:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [March 7, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/database-push-pull/184730/4 "2010-03-07T22:11:15Z")

</div>

On Sun, Mar 7, 2010 at 12:29 PM, DanC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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’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 🙂

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 🙂

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan
