产品型号具有attribute_1。 如果attribute_1需要重新计算,则before_validation将调用。 它给SystemStackError: stack level too deep因为self.save! 触发before_validation 。 如何阻止无限循环的回调。 before_validation :method_1, :if => :recalculation_required_attribute 我使用lock_version使用乐观锁定。 ‘update_all’不会增加lock_version 。 所以我正在使用save! 。 它呼唤回调的无限外观。 def method_1 #### #### if self.lock_version == Product.find(self.id).lock_version Product.where(:id => self.id).update_all(attributes) self.attributes = attributes self.save! end end
我的应用程序中有拍卖和出价对象,当有人按下BID BUTTON然后调用创建出价的BID CREATE控制器,然后在拍卖对象上做一些其他事情: BIDS控制器 – >创建 @auction.endtime += @auction.auctiontimer @auction.winner = @auction.arewinning @auction.save 拍卖模式 before_update :set_endtime def set_endtime self.endtime=self.starttime+self.auctiontimer end 所以问题是:在这个特定的@ auction.save中,C如何才能跳过“回调前”
我正在使用Rails 3.1.0,我想知道是否可以“同等”处理after_save和after_destroy回调。 也就是说,我需要为after_save和after_destroy回调运行相同的方法。 此时我必须单独处理这些回调,即使这些回调完成相同的事情: after_save do |record| # Make a thing end after_destroy do |record| # Make the same thing as in the ‘after_save’ callback end 那么, 有一种方法可以“同等”地处理after_save和after_destroy吗?
我在我的应用程序上安装了ActiveRecord信誉系统。 我如何重写或向评估模型添加回调/方法? 一般来说,如何为您安装的gem添加任何模型?
我有样本行,我想用#s替换前导#s。 (前两行)但是在string.gsub!(/^#+\w/, “”) ,我无法得到我想要替换的#的数量。 在javascript中,我可以使用带有replace方法的回调函数,但是如何归档这个Ruby呢? ##Command-line Tool ###Installment This is a ‘#’. 预期结果: ==Command-line Tool ===Installment This is a ‘#’.