Daniel Rikowski
Daniel Rikowski

Reputation: 72514

Can I perform a INSERT-SELECT operation with the Rails API?

I have to duplicate a BLOB field from one table into another and I want to use a INSERT-SELECT query to achive this.

INSERT INTO target_table (key, data, comment)
    SELECT 'my key', data, 'some comment' FROM source_table

Can this be done with the Rails API?

Of course I could always use ActiveRecord::Base.connection to send a native query to the database, but I'm hoping to find a "Rails way" to do this. (One which doesn't involve actually loading the data in my Rails application)

Upvotes: 10

Views: 2616

Answers (1)

karthiks
karthiks

Reputation: 7299

This is a typical scenario where using the SQL directly using ActiveRecord::Base.connection makes sense and sensibility. There can't possibly be any rails way to it as you described. Even if there were to be one, it has to load it in memory and insert it into the target table involving two models; this is insanity.

Upvotes: 4

Related Questions