# ActiveRecord Store of sessions in a separate database

**URL:** <https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593>\
**Category:** Rails\
**Created:** [August 21, 2007, 6:57am UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593 "2007-08-21T06:57:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 6:57am UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/1 "2007-08-21T06:57:13Z")

</div>

I’d like to be able to save sessions in a separate database.

I created the following model below in session.rb:

class Session \< ActiveRecord::Base  
self.abstract\_class = true  
establish\_connection :userdb  
end

I seem to be missing something however, as the server still tries to  
find  
the sessions table in the default database and not in the one I label as  
:userdb in my database.yml. What may that be?

Thanks in advance,

-Al

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 10:16am UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/2 "2007-08-21T10:16:36Z")

</div>

Hi,

Al wrote:

> class Session \< ActiveRecord::Base  
> self.abstract\_class = true  
> establish\_connection :userdb  
> end
> 
> I seem to be missing something however, as the server still tries to  
> find  
> the sessions table in the default database and not in the one I label as  
> :userdb in my database.yml. What may that be?

According to

[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001075](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001075)

you must write something like this:

establish\_connection(  
:adapter =\> “mysql”,  
:host =\> “localhost”,  
:username =\> “myuser”,  
:password =\> “mypass”,  
:database =\> “somedatabase”)

A symbol is not enough.

Lutz

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/3 "2007-08-21T15:16:24Z")

</div>

Actually, connecting to this database works from the console. That is,  
doing a simple Session.find(1) will display the right results. However,  
rails isn’t even referring to this model to know which database to  
connect  
to.

So, what I think is lacking is something that connects rails’ use of the  
actual session to the Session model that I created.

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/4 "2007-08-21T15:46:30Z")

</div>

Using a symbol works if the connection information is in database.yml.  
You supply the name of the connection only, allowing you to keep all  
database connections defined in one locaation.

Lutz H. wrote:

> > find  
> > establish\_connection(  
> > :adapter =\> “mysql”,  
> > :host =\> “localhost”,  
> > :username =\> “myuser”,  
> > :password =\> “mypass”,  
> > :database =\> “somedatabase”)
> 
> A symbol is not enough.
> 
> Lutz

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/5 "2007-08-21T16:00:34Z")

</div>

That didn’t seem to work as I got a

load\_missing\_constant’: uninitialized constant Session

when ran server.

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/6 "2007-08-21T15:52:32Z")

</div>

I believe you need the following in your environment.rb

config.action\_controller.session\_store = Session

You have to supply the name of your class that you wish to use. Hope  
this helps ya.

Al wrote:

> find the sessions table in the default database and not in the one I  
> label as :userdb in my database.yml. What may that be?
> 
> Thanks in advance,
> 
> -Al
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [August 21, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/7 "2007-08-21T18:25:13Z")

</div>

you will have to require it first since rails will not have loaded your  
class yet. just use a require above that line like:’

require File.join(File.dirname( **FILE** ), ‘app/models/session.rb’)

Al wrote:

> ```
> > I created the following model below in session.rb:
> > Thanks in advance,
> 
> ```
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [September 25, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/8 "2007-09-25T23:11:08Z")

</div>

Resolved this after updating my gems, and then running script/about and  
I  
saw that it wasn’t loading the file path correctly.  
So, my new problem is when reading session.rb, it says “uninitialized  
constant ActiveRecord”.

---

<div class="post-metadata">

**Author:** ![AlAlAl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AlAlAl](https://www.ruby-forum.com/u/AlAlAl)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/activerecord-store-of-sessions-in-a-separate-database/112593/9 "2007-09-25T23:10:22Z")

</div>

Odd… After adding that require line, I get the following error when  
launching script/server:

=\> Booting Mongrel (use ‘script/server webrick’ to force WEBrick)  
=\> Rails application starting on [http://0.0.0.0:3412](http://0.0.0.0:3412)  
=\> Call with -d to detach  
=\> Ctrl-C to shutdown server  
\*\* Starting Mongrel listening at 0.0.0.0:3412  
\*\* Starting Rails with development environment…  
Exiting  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:15:  
warning: already initialized constant OPTIONS  
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:18:  
undefined method `options' for []:Array (NoMethodError) from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:32:in `gem\_original\_require’  
from  
/usr/local/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:32:in  
`require' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’  
from  
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/dependencies.rb:342:in  
`new_constants_in' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’  
from /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3  
/lib/commands/server.rb:39  
from  
/usr/local/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
from ./script/server:3
