在轨道上的红宝石中的奇怪的activerecord行为

时间:2013-12-04 22:33:38

标签: ruby-on-rails ruby activerecord

拥有一个简单的潜在客户模型并尝试从字符串中清除\\t\\n

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

当我执行此操作时,我会一遍又一遍地获得更新

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拼写错误,并且是JSON字符串。


当我强行关闭更新循环时,输出

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!

2 个答案:

答案 0 :(得分:1)

我会使用squish

例如:

k = self.offense_description.squish

答案 1 :(得分:0)

尝试使用

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