I'm getting an error when trying to grab html from a webpage and saving it to my active record object. The message I'm getting is, Unclosed quotation mark after the character string
. Is there a function in Ruby or a way to go about checking and then correcting instances where this happens prior to saving the record to the database? The full error from TinyTDS is below...
ActiveRecord::StatementInvalid: TinyTds::Error: Unclosed quotation mark after the character string '
Update
My apologies, didn't realize I hadn't added enough information. I'll happily add any requests. Let me start with the log, and also to the part about "escaping my data" correctly -- It's HTML from an outside source that's throwing the error. So to that point I guess perhaps I go back to my initial question which was how do I go about locating where the unclosed quotation mark is, and then correcting it?
Error Log
SQL (27.8ms) BEGIN TRANSACTION
SQL (2.8ms) EXEC sp_executesql N'UPDATE [school_player_scrapes] SET [biography_text] = @0, [headshot_url] = @1, [updated_at] = @2 WHERE [school_player_scrapes].[id] = @3; SELECT @@ROWCOUNT AS AffectedRows', N'@0 nvarchar(max), @1 nvarchar(4000), @2 datetime, @3 int', @0 = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended Butte CC in 2015.</span></p>
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">High School:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.�</span></p>
</td></body></html>
', @1 = N'http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg', @2 = '12-15-2016 08:24:50.657', @3 = 7444 [["biography_text", "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><td class=\"sm PlayerBioArticleContent\" colspan=\"2\">\n\t\t\t\t<p><strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">Butte College:\xA0</span></strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">Attended Butte CC in 2015.</span></p>\r\n<p><strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">High School:\xA0</span></strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.\xA0</span></p>\n\t\t\t</td></body></html>\n"], ["headshot_url", "http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg"], ["updated_at", Thu, 15 Dec 2016 08:24:50 EST -05:00], ["id", 7444]]
TinyTds::Error: Unclosed quotation mark after the character string '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:'.: EXEC sp_executesql N'UPDATE [school_player_scrapes] SET [biography_text] = @0, [headshot_url] = @1, [updated_at] = @2 WHERE [school_player_scrapes].[id] = @3; SELECT @@ROWCOUNT AS AffectedRows', N'@0 nvarchar(max), @1 nvarchar(4000), @2 datetime, @3 int', @0 = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended Butte CC in 2015.</span></p>
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">High School:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.�</span></p>
</td></body></html>
', @1 = N'http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg', @2 = '12-15-2016 08:24:50.657', @3 = 7444
SQL (0.8ms) IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
Performed ScrapeSchoolRosterJob from Sidekiq(high_priority) in 2787.93ms
ActiveRecord::StatementInvalid: TinyTds::Error: Unclosed quotation mark after the character string '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:'.: EXEC sp_executesql N'UPDATE [school_player_scrapes] SET [biography_text] = @0, [headshot_url] = @1, [updated_at] = @2 WHERE [school_player_scrapes].[id] = @3; SELECT @@ROWCOUNT AS AffectedRows', N'@0 nvarchar(max), @1 nvarchar(4000), @2 datetime, @3 int', @0 = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended Butte CC in 2015.</span></p>
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">High School:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.�</span></p>
</td></body></html>
', @1 = N'http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg', @2 = '12-15-2016 08:24:50.657', @3 = 7444
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:336:in `each'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:336:in `handle_to_names_and_values_dblib'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:325:in `handle_to_names_and_values'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:300:in `_raw_select'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:295:in `block in raw_select'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:295:in `raw_select'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:244:in `sp_executesql'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:19:in `exec_query'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `exec_update'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:39:in `exec_update'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:114:in `update'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:88:in `_update_record'
... 57 levels...
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:337:in `call'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:337:in `block in simple'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_perform_callbacks'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/execution.rb:31:in `perform_now'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/execution.rb:16:in `perform_now'
from (irb):2
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
The way I was able to correct this issue, was converting my string I was trying to save from
ISO-8859-1
toUTF-8
by doing the following code:my_string.encode!('UTF-8')
. This removed the invalid characters that were causing and issue.