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 8465701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:07:32+00:00 2026-06-10T15:07:32+00:00

I have a system with a legacy database that I have to insert/update with

  • 0

I have a system with a legacy database that I have to insert/update with manual SQL. Right now, I have something like this:

class Legacy  < ActiveRecord::Base
  establish_connection("legacy#{Rails.env}")
end

class Book < Legacy
end

book = Book.find(params[:isbn])
# CLEAN UP BELOW
Legacy.connection.exec_query("
          UPDATE dbo.Book Set Title = #{Legacy.connection.quote(book.Title)}
          WHERE dbo.Book.ISBN = '#{book.ISNB}')")    

Any ideas how to clean that up? It’s a simplified example, in many cases there are multiple attributes in the Legacy table updated with one SQL statement.

I could add a new accessor

Set Title = #{book.Title!)

or simply:

Set Title = #{book.Title.send('quoted')}

Any other ideas?

UPDATE: here is an actual example:

result = Aim.connection.exec_query("
DECLARE @InsuredKey_PK int;
DECLARE @ResultVal varchar(125);
EXEC dbo.spAIMImportInsured #{tmpname}
, @InsuredKey_PK OUTPUT, @ResultVal OUTPUT, NULL, 'B', '#{submission.dba}', 
#{tmpaddr1}, #{tmpaddr2}, #{tmpcity}, '#{submission.state}', '#{submission.zip}' ,
#{tmpaddr1}, #{tmpaddr2}, #{tmpcity}, '#{submission.state}', '#{submission.zip}' 
, NULL, NULL, NULL, '#{submission.ProducerID}', 
'#{Employee.find(submission.acct_exec).first_name.downcase}',
'#{aim_name.Name}','#{aim_name.NameKeyPK}';
SELECT @InsuredKey_PK as insured_aim_key, @ResultVal as result;")

I sanitize like so:

class Insured < Legacy
  def self.update_attributes(id,update_hash)
    set_sql = ActiveRecord::Sanitization::ClassMethods.send(
      :sanitize_sql_for_assignment,update_hash
    )
    connection.exec_query("UPDATE dbo.Insured Set " + set_sql + 
      "WHERE dbo.Insured.InsuredID = #{id}"
    )
  end
end

called like this:

Insured.update_attributes(id,
  {:Address1 => params[:submission][:address1],
  :Address2 => params[:submission][:address2],
  :City => params[:submission][:city],
  :State => params[:submission][:state],
  :Zip =>params[:submission][:zip]
  })

I didn’t realize it when I first posted, but I’ve come to learn that my issue comes about when I insert/update data in the Legacy database from data stored in my regular Rails (MySQL) database. What I ended up doing was to put this method in all my Regular models:

def quoted_for_legacy(attribute)
  Legacy.connection.quote(self[attribute])
end

So now, when I’m updating Legacy from data stored in regular Rails MySQL models:

"Set Foo = #{regmodel.quoted_for_legacy(:Foo)}"
  • 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-10T15:07:34+00:00Added an answer on June 10, 2026 at 3:07 pm

    I use the Rails built-in method to do this. This lets you use all the convenient substitution shortcuts you can with other SQL in Rails. Just write a wrapper around the ActiveRecord method:

    def sanitize(*array)
        return ActiveRecord::Base.send(:sanitize_sql_array, array)
    end
    

    Note: Looks like this method has been moved in the latest version. If you’re using that, you’ll want ActiveRecord::Sanitization::ClassMethods#sanitize_sql_array instead.

    Then you can use the sanitize function whenever you need to sub variables into the SQL, just like you would normally. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm connecting to a legacy database that is our ERP system. I have a
I have a really ugly legacy database system that I need to integrate with.
I have a legacy system that I'd like to extend with an application written
We have a legacy system that requires a format which is not html but
At my place of work we have a legacy document management system that for
I currently have a legacy system that uses SPs exclusively for access to the
I have a legacy database with a pretty evil design that I need to
I have to insert some records in a table in a legacy database and,
I am exploring a legacy database system and have very little knowledge of its
I have created a data migration tool that reads data from a legacy SQL

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.