Forum: Ruby on Rails Fake ActiveRecord model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jan F. (Guest)
on 2006-03-27 14:28
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
Jeff J. (Guest)
on 2006-03-27 17:39
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.
Jan F. (Guest)
on 2006-03-27 18:24
> 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 ..
Ivan M. (Guest)
on 2006-03-27 19:00
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
Jan F. (Guest)
on 2006-03-27 22:32
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?).
Gerardo S. Gómez Garrido (Guest)
on 2006-03-28 02:11
(Received via mailing list)
2006/3/27, Jan F. <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
http://santanatechnotes.blogspot.com/
This topic is locked and can not be replied to.