Forum: Ruby on Rails how to execute create table on fly

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.
7c900f2569b4c1d253280ffc12eabecd?d=identicon&s=25 feng (Guest)
on 2007-04-06 10:19
dear all,


I'm a Rails newbie and i have some problem.

I'm using MySQL.

I want to excute some complexity query,

and I need to create some table to hold temp query result.

I want to know how to execute some SQL query such like

"CREATE TEMPORARY TABLE templist SELECT * FROM table2  "


I've tried to excute find_by_sql("create table templist select * ...),

but it seem to return a nil object , so that rails will halt.


any tips is appreciated.
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-06 11:46
I won't hold you back, but perhaps you should think about you solution -
temp tables are always strange.. Anyways,

http://api.rubyonrails.org/classes/ActiveRecord/Co...

ActiveRecord::Base.execute(sql, name=nil)

is your friend.
7c900f2569b4c1d253280ffc12eabecd?d=identicon&s=25 feng (Guest)
on 2007-04-06 15:25
Florian Gilcher wrote:
> I won't hold you back, but perhaps you should think about you solution -
> temp tables are always strange.. Anyways,
>
> 
http://api.rubyonrails.org/classes/ActiveRecord/Co...
>
> ActiveRecord::Base.execute(sql, name=nil)
>
> is your friend.

thanks. and I find "execute" in my "Agile" book.

However, I still don't know how to "execute" it in my controller.

if I add this line:

List.execute ("drop table if exists testtable;")


then i got a error:
"NoMethodError in LookupController#findcross

undefined method `execute' for List:Class"



Any tips is appreciated.

-Feng
Bef7ff8a0537495a1876ffebdc9f8e51?d=identicon&s=25 Lionel Bouton (Guest)
on 2007-04-06 15:38
(Received via mailing list)
feng wrote the following on 06.04.2007 15:25 :
>>
>
> thanks. and I find "execute" in my "Agile" book.
>
> However, I still don't know how to "execute" it in my controller.
>
> if I add this line:
>
> List.execute ("drop table if exists testtable;")
>

List.connection.execute should do it.
7c900f2569b4c1d253280ffc12eabecd?d=identicon&s=25 feng (Guest)
on 2007-04-06 15:42
Lionel Bouton wrote:
> feng wrote the following on 06.04.2007 15:25 :
>>>
>>
>> thanks. and I find "execute" in my "Agile" book.
>>
>> However, I still don't know how to "execute" it in my controller.
>>
>> if I add this line:
>>
>> List.execute ("drop table if exists testtable;")
>>
>
> List.connection.execute should do it.

it works!

thanks very very much !  :)
This topic is locked and can not be replied to.