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)
What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.
Object Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With