我想为我的费率操作创建一个after_save方法。它会将rating_score / ratings分开并更新列评级。
class KonkurrancersController < ApplicationController
def rate
@konkurrancer = Konkurrancer.find(params[:id])
@container = "Konkurrancer"+@konkurrancer.id.to_s
@konkurrancer.rating_score += params[:vind][:rating].to_i
@konkurrancer.ratings += 1
@konkurrancer.save
respond_to do |format|
format.js
end
end
end
这是我的模特:
class Konkurrancer < ActiveRecord::Base
after_save :do_foobar
private
def do_foobar
rating_score = self.rating_score
ratings = self.ratings
rating = (rating_score/ratings)
self.update_attributes(:rating => rating)
end
end
我的铁路日志:
Started POST "/konkurrancers/rate/46" for 127.0.0.1 at 2011-04-26 23:40:56 +0200
Processing by KonkurrancersController#rate as */*
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"MACFM37hX4S6XA9vryn7gtfl21P
vcaPBSiKDI8mfurg=", "vind"=>{"rating"=>"4"}, "id"=>"46"}
←[1m←[36mKonkurrancer Load (1.0ms)←[0m ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers`←[0m
←[1m←[35mCACHE (0.0ms)←[0m SELECT `konkurrancers`.* FROM `konkurrancers`
←[1m←[36mCACHE (0.0ms)←[0m ←[1mSELECT `konkurrancers`.* FROM `konkurrancers`←
[0m
←[1m←[35mKonkurrancer Load (1.0ms)←[0m SELECT `konkurrancers`.* FROM `konkurr
ancers` WHERE (`konkurrancers`.`cached_slug` = '46') LIMIT 1
←[1m←[36mSQL (2.0ms)←[0m ←[1mSELECT sluggable_id FROM slugs WHERE ((slugs.slu
ggable_type = 'Konkurrancer' AND slugs.name = '46' AND slugs.sequence = 1))←[0m
←[1m←[35mKonkurrancer Load (1.0ms)←[0m SELECT `konkurrancers`.* FROM `konkurr
ancers` WHERE (`konkurrancers`.`id` = 46) LIMIT 1
←[1m←[36mSQL (0.0ms)←[0m ←[1mBEGIN←[0m
←[1m←[35mLink Load (1.0ms)←[0m SELECT `links`.* FROM `links` WHERE (`links`.k
onkurrancer_id = 46) LIMIT 1
←[1m←[36mSQL (0.0ms)←[0m ←[1mROLLBACK←[0m
Rendered konkurrancers/_rating.html.erb (1.0ms)
Rendered konkurrancers/rate.js.erb (22.0ms)
Completed 200 OK in 606ms (Views: 286.0ms | ActiveRecord: 6.0ms)
我该如何创建呢?
答案 0 :(得分:67)
update_attribute
回调中的任何after_save
都会导致Rails3 +中的递归。
应该做的是:
after_save :updater
# Awesome Ruby code
# ...
# ...
private
def updater
self.update_column(:column_name, new_value) # This will skip validation gracefully.
end
答案 1 :(得分:11)
你想要的是一个回调。您可以在Konkurrancer模型上创建一个after_save回调,该模型在为该模型调用 save()方法后触发。
例如:
class Konkurrancer < ActiveRecord::Base
after_save :do_foobar
private
def do_foobar
rating_score = self.rating_score
ratings = self.ratings
rating = (rating_score/ratings)
self.update_attributes(:ratings => rating)
end
end
[编辑] 您应该使用自我,因为您正在编辑的模型是模型本身。测试它,并应用必要的逻辑/实现。
有关详细信息,请查看此guide。
希望有所帮助!
答案 2 :(得分:9)
不确定为什么人们会回答错误的答案并低估正确答案。
Zakelfassi是正确的,而且基督徒法齐尼对Rails 3及以上版本是错误的。如果你在保存回调中执行#update_attributes,你会进入无休止的递归。你想按照他的例子做#update_column。
亲自试试,你会看到。
答案 3 :(得分:0)
@konkurrancer.update_attributes :ratings=>'updated value'
答案 4 :(得分:0)