# Multiple Database write question

**URL:** <https://www.ruby-forum.com/t/multiple-database-write-question/98001>\
**Category:** Rails\
**Created:** [January 27, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/multiple-database-write-question/98001 "2006-01-27T18:58:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 27, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/multiple-database-write-question/98001/1 "2006-01-27T18:58:05Z")

</div>

I am working on reconfiguring an app that we are about to release to  
work tighter with another app that we already have. Both of these  
apps are going to share a single users database that stores the  
general information about the user such as login, password, name,  
email and so forth. They also have app specific stuff that is  
attributed to the user that is stored in the app’s own db. My  
question is this, I have tried several ways to get a single object to  
be created from data coming from different DB’s but could not get it  
to work so i went about having a couple of different objects and  
reworking the various actions to pull data from them. However I ran  
into an issue that I can’t wrap my head around.

The User model has a establish\_connection set to my shared users  
database and I can read data all day long from it and everything  
works fine, however when I go to allow the user to edit their profile  
(change prefered language, or their email) the app hapily sends the  
data to the controller, but the info is never saved into the  
database. Looking through the logs nothing ever complains either.  
Where things are really getting me is that on a different box, where  
we have not made these changes yet if i go through the same process  
the app reacts as it should. It is my understanding that when  
connecting to another DB you do it in the model and than it should  
connect and update what needs to be changed.

My controller looks like this:

def info  
@updateuser = nil  
if @request.post?  
@updateuser = User.find\_first([“login = ?”, @user.login])  
@updateuser.lastname = @params[:post][:lastname]  
@updateuser.firstname= @params[:post][:firstname]  
@updateuser.prefered\_language = @params[:prefered\_language]  
notice = “”  
if not @params[:post][:password].empty? and not @params[:post]  
[:passwordbis].empty?  
if @params[:post][:password] == @params[:post][:passwordbis]  
@newuser.password = @params[:post][:password]  
else  
notice += “Your passwords don’t match!”  
end  
end  
if not notice.empty?  
flash[:notice] = notice  
redirect\_back\_or\_default :controller =\> “podcasts”  
else  
if not @updateuser.nil? and @updateuser.save  
flash[:notice] = \_(‘account\_prefs\_saved’)  
redirect\_back\_or\_default :controller =\> “articles”  
@user = @updateuser  
else  
flash.now[:notice] = “An error occured while saving your preferences”  
redirect\_back\_or\_default :controller =\> “articles”  
end  
end  
end

and my model has the following connection code in it:

establish\_connection(:adapter =\> “mysql”,  
:host =\> “localhost”,  
:port =\> 3306,  
:database =\> “GlobalUsers\_development”,  
:username =\> “root”,  
:password =\> “root”)  
set\_table\_name “users”

as you can see right now on my dev box I am connecting to my local  
mysql instance as root so it shouldn’t be permission problems.

Do I need to establish a connection in the controller as well to  
update this info? My understanding of the Various Howto’s that I have  
found only mention putting the code in the model to connect.

Aslo I have tried to put the connection info in my database.yml file  
but rails complains left and right when I try to access the app that  
the database is not initialized.

thanks for all the help

Andrew

---

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [January 29, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/multiple-database-write-question/98001/2 "2006-01-29T18:49:14Z")

</div>

Can someone please help me figure this out. I did some playing around  
and if I change my below code to something like this:

@updateuser.update\_attribute(:prefered\_language, @params  
[:prefered\_language])

and leave everything else alone it works for the most part except  
that I lose my flash notice that everything was saved in the users  
preferences. However If I leave it as it was it doesn’t work when  
going after my new database but if the user table is in the existing  
app database it works as it should. (proven by the current production  
server of the app). I thought if I explicitly put a @updateuser.save  
in there it would work however it does not, no matter where I put a  
@updateuser.save the @updateuser object does not update the  
associated table for that user. I really would like to get it working  
the way that I have it right now but would make the change if I  
absolutely must, as like I said it worked before I switched to moving  
the user info to a second database.

Any thoughts would be much appreciated.

Andrew
