Austio
Austio

Reputation: 6075

Wierd active record behavior in ruby on rails

Have a simple Lead model and trying to clean out the \\t and \\n from a string

class Lead < ActiveRecord::Base


attr_accessible :offense_description

def clean_offense_description
  if self.offense_description
    k = self.offense_description.gsub("\\t", '').gsub("\\n", '')
    self.update_attributes({:offense_description => k},:without_protection => true)
  end
end

When I execute this i get the update over and over and over

irb(main):004:0> l.clean_offense_description
(58.7ms)  BEGIN
(59.7ms)  UPDATE `leads` SET `offense_description` = '{\"1\":{\"description\":\"Some Description }\",\"code\":\"234\",\"agency\":\"SO JASPER COUNTY - CARTHAGE\"}}', `updated_at` = '2013-12-04 22:27:31' WHERE `leads`.`id` = 440919

Offense_description is spelled wrong, and is a JSON string.


When i forcibly close the loop of updates here is the output

ActiveRecord::StatementInvalid: IRB::Abort: abort then interrupt!: UPDATE `leads` SET `offense_description` = '{\"1\":{\"description\":\"Non-Support\",\"code\":\"500\",\"agency\":\"SO\"}}', `updated_at` = '2013-12-05 05:06:29' WHERE `leads`.`id` = 440919
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:420:in `raise'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:420:in `irb_abort'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:607:in `signal_handle'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:392:in `block in start'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `call'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:238:in `exec_delete'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:96:in `update'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/activerecord-3.2.13/lib/active_record/persistence.rb:359:in `update'
... 1622 levels...
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb/ruby-lex.rb:247:in `block (2 levels) in each_top_level_statement'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb/ruby-lex.rb:233:in `loop'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb/ruby-lex.rb:233:in `block in each_top_level_statement'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb/ruby-lex.rb:232:in `catch'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb/ruby-lex.rb:232:in `each_top_level_statement'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:488:in `eval_input'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:397:in `block in start'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:396:in `catch'
from /home/talos/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/irb.rb:396:in `start'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /home/talos/.rvm/gems/ruby-2.0.0-p0@LegalLeads/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from /home/talos/Link to RubymineProjects/legalleads/script/rails:6:in `require'
from /home/talos/Link to RubymineProjects/legalleads/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'Maybe IRB bug!

Upvotes: 1

Views: 720

Answers (2)

Joseph Wahba
Joseph Wahba

Reputation: 439

Try to use

self.offense_description.gsub(/\t/, '').gsub(/\n/, '')

Upvotes: 0

beck03076
beck03076

Reputation: 3308

I would use squish.

For example:

k = self.offense_description.squish

Upvotes: 1

Related Questions