为什么`around_save`或`after_touch`回调不会在创建新记录时执行?

时间:2012-11-27 00:01:06

标签: ruby-on-rails ruby-on-rails-3

我正在尝试在创建/更新记录的任何时候执行方法。

我尝试了after_toucharound_save,但都没有用于创建新记录。

事实上,around_save在编辑记录时不起作用。

我可以使用什么回调来保存记录(包括创建时或编辑/更新时)?

修改1

以下是一些代码:

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

未执行的回调是最后一个calculate_weighted_score

def calculate_weighted_score
    # Sum products of weight & scores of each attribute
        weight = Weight.first
        score = self.score
        self.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)
        # self.update_attributes(:weighted_score => weighted_score)
end

这是edit操作的日志:

Started PUT "/clients/20" for 127.0.0.1 at 2012-11-26 23:26:34 -0500
Processing by ClientsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"J172LuZQc5SPv8=", "client"=>{"name"=>"Jackie Boys", "email"=>"jack@welch.com", "phone"=>"1234567890", "firm_id"=>"2", "topic_ids"=>["1", "2", "10"], "personal_priority"=>"1", "last_contact"=>"2012-09-01", "vote"=>"1", "vote_for_user"=>"1", "next_vote"=>"2012-12-01", "vote_ii"=>"0", "vote_ii_for_us"=>"0"}, "commit"=>"Update Client", "id"=>"20"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Client Load (0.1ms)  SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = 1 AND "clients"."id" = ? LIMIT 1  [["id", "20"]]
  Topic Load (0.1ms)  SELECT "topics".* FROM "topics" 
   (0.0ms)  begin transaction
  Topic Load (0.2ms)  SELECT "topics".* FROM "topics" WHERE "topics"."id" IN (1, 2, 10)
  Topic Load (0.1ms)  SELECT "topics".* FROM "topics" INNER JOIN "clients_topics" ON "topics"."id" = "clients_topics"."topic_id" WHERE "clients_topics"."client_id" = 20
   (0.8ms)  UPDATE "clients" SET "name" = 'Jackie Boys', "updated_at" = '2012-11-27 04:26:34.772771' WHERE "clients"."id" = 20
  Firm Load (0.1ms)  SELECT "firms".* FROM "firms" WHERE "firms"."id" = 2 LIMIT 1
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Score Load (0.2ms)  SELECT "scores".* FROM "scores" WHERE "scores"."user_id" = 1 LIMIT 1
  SalesTeam Load (0.1ms)  SELECT "sales_teams".* FROM "sales_teams" WHERE "sales_teams"."id" = 3 LIMIT 1
  PriorityLevel Load (0.1ms)  SELECT "priority_levels".* FROM "priority_levels" WHERE "priority_levels"."id" = 1 LIMIT 1
   (0.3ms)  UPDATE "scores" SET "client_id" = 20, "firm_size" = 102.0, "inflection_point" = 0.0, "sales_priority" = 1.0, "days_since_contact" = 86.0, "did_client_vote_for_us" = 1.0, "days_until_next_vote" = 5.0, "does_client_vote_ii" = 0.0, "did_client_vote_ii_for_us" = 0.0, "updated_at" = '2012-11-27 04:26:35.043986' WHERE "scores"."id" = 26
  Score Load (0.2ms)  SELECT "scores".* FROM "scores" WHERE "scores"."client_id" = 20 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" = 20 LIMIT 1
  Weight Load (0.2ms)  SELECT "weights".* FROM "weights" LIMIT 1
   (52.4ms)  commit transaction
Redirected to http://localhost:3000/clients
Completed 302 Found in 505ms (ActiveRecord: 60.7ms)

请注意,您应该看到的是更新client.weighted_score但未显示在日志中的语句。

当我添加self.update_attributes...时,我得到了stack is too deep error。当我添加self.save而不是self.update_attributes时会发生同样的事情。

0 个答案:

没有答案