レコードが作成/更新されるたびにメソッドを実行しようとしています。
私は両方を試してみましたがafter_touch
、around_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
ます。