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 -- Posted via http://www.ruby-forum.com/.
Jan Foeh 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, > JanWhen 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. -- Posted via http://www.ruby-forum.com/.
> 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 .. -- Posted via http://www.ruby-forum.com/.
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 Foeh 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-- Posted via http://www.ruby-forum.com/.
Ivan Medda 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?). -- Posted via http://www.ruby-forum.com/.
2006/3/27, Jan Foeh <jan.foeh@parship.de>:> Ivan Medda 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).In that case, you don''t need an ORM. Just use the respective driver for your DBMS. -- Gerardo Santana "Between individuals, as between nations, respect for the rights of others is peace" - Don Benito Ju?rez http://santanatechnotes.blogspot.com/