2

I am currently working on RoR with PostgreSQL.

Basically, ActiveRecord satisfies most of the requirement of data retrieval. However, in some cases, it seems that using one statement would be much more efficient.

Therefore, is it possible to use one prepared statement to do multiple insert as the followings? Or I have to append the raw sql statement myself?

INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy'), ...;

2 Answers 2

1

try this

user_string = " ('code1','title1', 'aaa', ...), ('code2','title2'...)"

User.connection.insert("INSERT INTO films (code, title, did, date_prod, kind)VALUES"+user_string) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thus, I cannot achieve it by prepared statements, right?
Not for multiple rows.
1

After two years of experience working on RoR,

There is a gem called activerecord-import that can do the thing. And here is the example code.

books = Array.new
books << Book.new(title: "Programming Ruby", author: "Dave Thomas")
books << Book.new(title: "Agile Web Development with Rails 5", author: "Sam Ruby")
Book.import books

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.