开发者

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

开发者 https://www.devze.com 2023-02-05 22:56 出处:网络
I have to duplicate a BLOB field from one table into another and I want to use a INSERT-SELECT query to achive this.

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开发者_StackOverflow 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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消