# Connect to db table without needing blank models?

**URL:** <https://www.ruby-forum.com/t/connect-to-db-table-without-needing-blank-models/105550>\
**Category:** Rails\
**Created:** [May 31, 2007, 1:15am UTC](https://www.ruby-forum.com/t/connect-to-db-table-without-needing-blank-models/105550 "2007-05-31T01:15:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [May 31, 2007, 1:15am UTC](https://www.ruby-forum.com/t/connect-to-db-table-without-needing-blank-models/105550/1 "2007-05-31T01:15:49Z")

</div>

Hi,

I’m wanting to get data from a table called ea\_service\_provider, and  
have managed to do so by creating a blank file called  
ea\_service\_provider.rb and with the following contents:

class EaServiceProvider \< ActiveRecord::Base

# model to interact with ea\_service\_provider table

end

This is so in another controller I can get the list of service  
providers by:

@ea\_service\_providers = EaServiceProvider.find(:all);

I’m kind of unhappy having this file around just to interact with the  
table. Can I use Rails to connect to an arbitrary table without  
needing to create blank models?

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [May 31, 2007, 1:28am UTC](https://www.ruby-forum.com/t/connect-to-db-table-without-needing-blank-models/105550/2 "2007-05-31T01:28:49Z")

</div>

If you find you have a lot of them, this might be worthwhile:

[http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/](http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/)

Cheers,  
Tyler

luke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [May 31, 2007, 3:35am UTC](https://www.ruby-forum.com/t/connect-to-db-table-without-needing-blank-models/105550/3 "2007-05-31T03:35:59Z")

</div>

If you want to use ActiveRecord to access your table then you need a  
model. You can always create the model class on the fly and throw it  
away, but you need it.

If you just want to do an SQL query and do not need ruby objects back,  
just do a query using the connection from any model class in the same  
database.

model.connection.select\_all(sql)

Michael
