1

更新 2

顺便说一句,当我打字时, Update 1@muffinista 发布了我得出的相同结论Update 1,我只是想澄清一下,我不是在扼杀他的答案并随机更新问题。在发布问题后,我仔细考虑了一些问题,我意识到问题出在哪里。虽然@muffinista 发布了原因,但他的解决方案是不充分的——这就是我保持Update 1原样的原因——因为我找到了一个更好的解决方案,并且其他人获得完整的上下文是有意义的。

更新 1

所以我想出了是什么导致了这个错误,一个无限循环。

我正在尝试将记录保存在客户端记录clientafter_save回调中。所以它不断尝试保存客户端记录并执行尝试保存客户端记录的 after_save 回调。

我怎样才能实现我想要的,即client.weighted_score每当更新记录时更新这个属性而不跳入这个循环?

原始问题

我有这个回调:

after_save :calculate_weighted_score, :if => Proc.new { |c| c.score.present? }

def calculate_weighted_score
    # Sum products of weight & scores of each attribute
        client = self
        weight = Weight.first
        score = self.score
        client.weighted_score = (weight.firm_size * score.firm_size) + (weight.priority_level * score.priority_level) + 
                (weight.inflection_point * score.inflection_point) + (weight.personal_priority * score.personal_priority) +
                (weight.sales_priority * score.sales_priority) + (weight.sales_team_priority * score.sales_team_priority) +
                (weight.days_since_contact * score.days_since_contact) + (weight.does_client_vote * score.does_client_vote) +
                (weight.did_client_vote_for_us * score.did_client_vote_for_us) + (weight.days_until_next_vote * score.days_until_next_vote) +
                (weight.does_client_vote_ii * score.does_client_vote_ii) + (weight.did_client_vote_ii_for_us * score.did_client_vote_ii_for_us) + 
                (weight.days_until_vote_ii * score.days_until_vote_ii)
        client.o
            # self.save
        # client.update_attributes(:weighted_score => weighted_score)
end

这是运行此回调之前客户端记录状态的示例:

#<Client:0x007fe00dbcea90> {
                   :id => 10,
                 :name => "Manta-Jar Gale",
                :email => "mj@gmail.com",
                :phone => 8769876435,
              :firm_id => 1,
           :created_at => Fri, 23 Nov 2012 23:50:09 UTC +00:00,
           :updated_at => Tue, 27 Nov 2012 17:50:01 UTC +00:00,
              :user_id => 1,
    :personal_priority => true,
         :last_contact => Sat, 08 Jan 2011,
                 :vote => true,
        :vote_for_user => false,
            :next_vote => Thu, 02 Jan 2014,
              :vote_ii => true,
       :vote_ii_for_us => true,
         :next_vote_ii => Mon, 01 Jul 2013,
       :weighted_score => nil,
            :firm_size => 100.0
}

注意weighted_score => nil属性。

回调之后,同样的记录如下所示:

#<Client:0x007fe00dbcea90> {
                   :id => 10,
                 :name => "Manta-Jar Gale",
                :email => "mj@gmail.com",
                :phone => 8769876435,
              :firm_id => 1,
           :created_at => Fri, 23 Nov 2012 23:50:09 UTC +00:00,
           :updated_at => Tue, 27 Nov 2012 17:50:01 UTC +00:00,
              :user_id => 1,
    :personal_priority => true,
         :last_contact => Sat, 08 Jan 2011,
                 :vote => true,
        :vote_for_user => false,
            :next_vote => Thu, 02 Jan 2014,
              :vote_ii => true,
       :vote_ii_for_us => true,
         :next_vote_ii => Mon, 01 Jul 2013,
       :weighted_score => 9808,
            :firm_size => 100.0
}

注意weighted_score => 9808属性。

所以我知道回调calculate_weighted_score正在运行,整个回调似乎是正确的,直到分配client.weighted_score. 问题是,日志显示UPDATE该属性没有数据库事务:

Started PUT "/clients/10" for 127.0.0.1 at 2012-11-27 12:50:01 -0500
Processing by ClientsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"J172LuZQ=", "client"=>{"name"=>"Manta-Jar Gale", "email"=>"mj@gmail.com", "phone"=>"8769876435", "firm_id"=>"1", "topic_ids"=>["1", "2", "9"], "personal_priority"=>"1", "last_contact"=>"2011-01-08", "vote"=>"1", "vote_for_user"=>"0", "next_vote"=>"2014-01-02", "vote_ii"=>"1", "vote_ii_for_us"=>"1"}, "commit"=>"Update Client", "id"=>"10"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Client Load (0.2ms)  SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = 1 AND "clients"."id" = ? LIMIT 1  [["id", "10"]]
  Topic Load (0.3ms)  SELECT "topics".* FROM "topics" 
   (0.1ms)  begin transaction
  Topic Load (0.2ms)  SELECT "topics".* FROM "topics" WHERE "topics"."id" IN (1, 2, 9)
  Topic Load (0.1ms)  SELECT "topics".* FROM "topics" INNER JOIN "clients_topics" ON "topics"."id" = "clients_topics"."topic_id" WHERE "clients_topics"."client_id" = 10
   (0.5ms)  UPDATE "clients" SET "name" = 'Manta-Jar Gale', "updated_at" = '2012-11-27 17:50:01.856893' WHERE "clients"."id" = 10
  Firm Load (0.2ms)  SELECT "firms".* FROM "firms" WHERE "firms"."id" = 1 LIMIT 1
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Score Load (0.2ms)  SELECT "scores".* FROM "scores" WHERE "scores"."client_id" = 10 LIMIT 1
  SalesTeam Load (0.1ms)  SELECT "sales_teams".* FROM "sales_teams" WHERE "sales_teams"."id" = 2 LIMIT 1
  PriorityLevel Load (0.1ms)  SELECT "priority_levels".* FROM "priority_levels" WHERE "priority_levels"."id" = 1 LIMIT 1
  CACHE (0.0ms)  SELECT "scores".* FROM "scores" WHERE "scores"."client_id" = 10 LIMIT 1
  Max Load (0.2ms)  SELECT "maxes".* FROM "maxes" WHERE "maxes"."user_id" = 1 LIMIT 1
  CACHE (0.0ms)  SELECT "scores".* FROM "scores" WHERE "scores"."client_id" = 10 LIMIT 1
  Weight Load (0.2ms)  SELECT "weights".* FROM "weights" LIMIT 1
   (3.2ms)  commit transaction
Redirected to http://localhost:3000/clients
Completed 302 Found in 796ms (ActiveRecord: 26.4ms)

唯一的UPDATE交易是对我为测试回调所做的名称进行编辑。

我知道没有UPDATE交易,因为从技术上讲,我没有保存记录。

但是,当我尝试执行任何已注释掉的语句时——即client.save,或者client.update_attributes(....)我得到一个Stack Level Too Deep错误。

是什么原因造成的,如何保存此记录?

4

3 回答 3

3

正如@muffinista 所指出的(并在问题本身的更新中指定),这里的问题是回调正在尝试保存正在调用回调的记录,该回调正在尝试保存记录...创建一个Stack Overflow(偷了那个@ivan 关于这个类似问题的笑话)。

似乎最好的答案是使用update_column-可以在此处查看文档

这与做的事情相同update_attributes,但不做验证或回调——这对我在这个特定实例中想要的很好。

需要特别说明的update_column是, 的格式与update_attributes.

你要做的是:

client.update_column(:weighted_score, weighted_score)

:weighted_score我的列名在哪里,并且weighted_score是我在上面的代码示例中设置的为我计算的局部变量。

对于它的价值,我从这个 SO answer得到了这个答案。

于 2012-11-27T18:30:07.150 回答
1

保存原始记录后,您的回调正在运行,但您正在修改记录并尝试重新保存它,这会无限期地再次触发回调。这基本上是一个重复的问题,例如在这里介绍:保存后何时更新记录?

于 2012-11-27T18:07:47.850 回答
1

改用 before_save 过滤器怎么样?

于 2012-11-27T18:18:45.347 回答