# Fake ActiveRecord model?

**URL:** <https://www.ruby-forum.com/t/fake-activerecord-model/87581>\
**Category:** Rails\
**Created:** [March 27, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581 "2006-03-27T12:28:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 27, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581/1 "2006-03-27T12:28:50Z")

</div>

Hi all,

I have a legacy table which I cannot get to work with ActiveRecord (no  
ids etc). All I want is to create a class which opens a connection  
manually and allows me to execute some plain SQL statements.

What I can’t figure out is how to either build up a connection and  
execute queries from a non-AR inherited class, or how to create a  
AR-inherited class minus all the AR magic and methods which won’t work  
on this table.

Thanks in advance for any help,  
Jan

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 27, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581/2 "2006-03-27T15:39:47Z")

</div>

Jan F. wrote:

> Hi all,
> 
> I have a legacy table which I cannot get to work with ActiveRecord (no  
> ids etc). All I want is to create a class which opens a connection  
> manually and allows me to execute some plain SQL statements.
> 
> What I can’t figure out is how to either build up a connection and  
> execute queries from a non-AR inherited class, or how to create a  
> AR-inherited class minus all the AR magic and methods which won’t work  
> on this table.
> 
> Thanks in advance for any help,  
> Jan

When you say “No ids” is this the only reason? You can use  
set\_table\_name and set\_primary key in the model definition to give it  
non Rails names. I have created a Rails application that works on a  
legacy database where every model has done this.

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 27, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581/3 "2006-03-27T16:24:16Z")

</div>

> When you say “No ids” is this the only reason? You can use  
> set\_table\_name and set\_primary key in the model definition to give it  
> non Rails names. I have created a Rails application that works on a  
> legacy database where every model has done this.

I’ve been using that for a lot of other tables in that legacy database,  
and it works well, even with non-numeric IDs. Unfortunately, this table  
uses composite primary keys …

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 27, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581/4 "2006-03-27T17:00:16Z")

</div>

Hi,  
I had the same problem.  
I resolve all problems usind the “set\_table\_name” command ,that forge  
the compiler to use the right model.

Jan F. wrote:

> Hi all,
> 
> I have a legacy table which I cannot get to work with ActiveRecord (no  
> ids etc). All I want is to create a class which opens a connection  
> manually and allows me to execute some plain SQL statements.
> 
> What I can’t figure out is how to either build up a connection and  
> execute queries from a non-AR inherited class, or how to create a  
> AR-inherited class minus all the AR magic and methods which won’t work  
> on this table.
> 
> Thanks in advance for any help,  
> Jan

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 28, 2006, 12:11am UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581/5 "2006-03-28T00:11:44Z")

</div>

2006/3/27, Jan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> establish a connection within the class and execute raw queries over it.  
> I see how to establish the connection, but I don’t understand how to  
> access it afterwards, since the above code doesn’t work (I know  
> establish\_connection doesn’t return connection handlers, it’s just what  
> I would have done in PHP).

In that case, you don’t need an ORM. Just use the respective driver  
for your DBMS.

–  
Gerardo S.  
“Between individuals, as between nations, respect for the rights of  
others is peace” - Don Benito Juárez

> **[Santana's Tech Notes](http://santanatechnotes.blogspot.com/)**
>
> Small notes about technical related stuff I do, mainly OpenBSD and Ruby

---

<div class="post-metadata">

**Author:** ![janfoeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@janfoeh](https://www.ruby-forum.com/u/janfoeh)\
**Post date:** [March 27, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/fake-activerecord-model/87581/6 "2006-03-27T20:32:27Z")

</div>

Ivan M. wrote:

> I had the same problem.  
> I resolve all problems usind the “set\_table\_name” command ,that forge  
> the compiler to use the right model.

Thanks Ivan, but the table name isn’t really my problem, since the model  
name actually matches the table name (‘Contact’ -\> ‘contacts’).

Since there doesn’t seem to be a way to have AR deal with my ugly  
composite-primary-key-table directly, I’m only looking for a way to  
establish a connection within the class and execute raw queries over it.  
In pseudo-code:

class Contacts  
connection\_handler = ActiveRecord::Base.establish\_connection(:foobar)  
def find(foo,bar)  
connection\_handler.execute(“SELECT \* FROM contacts WHERE a = ? and  
b = ?”, foo, bar)  
end  
end

I see how to establish the connection, but I don’t understand how to  
access it afterwards, since the above code doesn’t work (I know  
establish\_connection doesn’t return connection handlers, it’s just what  
I would have done in PHP).

So, I’d still be thankful for any insights on how to deal with this (or  
maybe even how to work around composite primary keys within AR?).
