Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:40:55+00:00 2026-06-12T13:40:55+00:00

I have an Evaluation model. Evaluation has many scores. Whenever a new evaluation is

  • 0

I have an Evaluation model. Evaluation has many scores. Whenever a new evaluation is created, a score record is created for each user that needs an evaluation (see below for the current method I am using to do this.) So, for example, 40 score records might be created at once. The Evaluation owner then updates each score record with the User’s score.

I’m looking to use raw SQL because each insert is its own transaction and is slow.

I would like to convert the following into a mass insert statement using raw SQL:

def build_evaluation_score_items
  self.job.active_employees.each do |employee|
    employee_score = self.scores.build
    employee_score.user_id = employee.id
    employee_score.save
  end
end

Any thoughts on how this can be done? I’ve tried adapting a code sample from Chris Heald’s Coffee Powered site but, no dice.

Thanks to anyone willing to help!

EDIT 1

I neglected to mention the current method is wrapped in a transaction.

So, essentially, I am trying to add this to the code block so everything is inserted in one statement (** This code snippit is from Chris Heald’s Coffee Powered site that discussed the topic. I would ask the question there but the post is > 3 yrs old.):

inserts = []
TIMES.times do
  inserts.push "(3.0, '2009-01-23 20:21:13', 2, 1)"
end
sql = "INSERT INTO user_node_scores (`score`, `updated_at`, `node_id`, `user_id`)VALUES #{inserts.join(", ")}"

I’d be happy to show the code from some of my attempts that do not work…

Thanks again!

Well, I’ve cobbled together something that resembles the code above but I get a SQL statement invalid error around the (‘evaluation_id’ portion. Any thoughts?

def build_evaluation_score_items
  inserts = []
  self.job.active_employees.each do |employee|
    inserts.push "(#{self.id}, #{employee.id}, #{Time.now}, #{Time.now})"
  end
  sql = "INSERT INTO scores ('evaluation_id', `user_id`, 'created_at', `updated_at`)VALUES #{inserts.join(", ")}"
  ActiveRecord::Base.connection.execute(sql) 
end

Any idea as to what in the above SQL code is causing the error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:40:57+00:00Added an answer on June 12, 2026 at 1:40 pm

    Well, after much trial and error, here is the final answer. The cool thing is that all the records are inserted via one statement. Of course, validations are skipped (so this will not be appropriate if you require model validations on create) but in my case, that’s not necessary because all I’m doing is setting up the score record for each employee’s evaluation. Of course, validations work as expected when the job leader updates the employee’s evaluation score.

    def build_evaluation_score_items
      inserts = []
      time = Time.now.to_s(:db)
      self.job.active_employees.each do |employee|
        inserts.push "(#{self.id}, #{employee.id}, '#{time}')"
      end
      sql = "INSERT INTO scores (evaluation_id, user_id, created_at) VALUES #{inserts.join(", ")}"
      ActiveRecord::Base.connection.execute(sql) 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a model in Django that has multiple pictures associated
I have 2 models, User and Evolution. User has many Evolution, Evolution belongs to
I have created a website using modx evolution v1.0.2. The website that I have
I have made an openGL camera class that uses lazy evaluation to provide the
I have a User model, an Event Model, an Event Priority Model and a
I have a django model which has a load of relatively small fields and
Have spend quite some time to learn Sencha touch, just like an evaluation about
I have a single product table with multiple fields which contain user evaluations of
I have a node.js (v0.6.12) application that starts by evaluating a Javascript file, startup.js.
I have a Question domain model designed as follows class Question { List<Choice> choiceCollection;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.